Nginx invalid URL prefix - nginx

I have a really basic nginx setup which is failing for some reason;
server {
listen 80;
server_name librestock.com;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/david/StockSearch/stocksearch;
}
location / {
include proxy_params;
proxy_pass unix:/home/david/StockSearch/stocksearch/stocksearch.sock;
}
}
according to everything I've read I'm setting the server name correctly.
when I replace librestock.com with the ip of the server it works.
error:
$ nginx -t
nginx: [emerg] invalid URL prefix in /etc/nginx/sites-enabled/stocksearch:12
nginx: configuration file /etc/nginx/nginx.conf test failed

You need the http:// prefix on your unix: path, as in:
proxy_pass http://unix:/home/david/StockSearch/stocksearch/stocksearch.sock;
See
http://nginx.org/en/docs/http/ngx_http_proxy_module.html

Related

nginx http server location include unknown directive error

my nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
upstream app_servers {
server 127.0.0.1:5000;
server 127.0.0.1:5001;
}
server {
listen 6200;
server_name test;
add_header X-GG-Cache-Status $upstream_cache_status;
include rewrite.conf;
}
}
and my rewrite.conf in the same folder as that
location = / {
rewrite ^/some-custom-destination/?$ /destination/detail?id=33;
proxy_pass http: //app_servers;
proxy_intercept_errors on;
error_page 400 404 /;
error_page 500 502 503 504 /error.html;
location = /error.html {
root /etc/nginx/;
}
}
when I use nginx -s reload command getting that error : nginx: [emerg] unknown directive "location" in /etc/nginx/rewrite.conf:1
How can I fix that?
Help, please. Thank you.
Apart from the space in your proxy_pass directive, there is one more issue with your location block.
From the nginx documentation about nginx location directive (http://nginx.org/en/docs/http/ngx_http_core_module.html#location), you cannot have a nested location inside a location block “location = /”.
“Also, using the “=” modifier it is possible to define an exact match of URI and location. If an exact match is found, the search terminates. For example, if a “/” request happens frequently, defining “location = /” will speed up the processing of these requests, as search terminates right after the first comparison. Such a location cannot obviously contain nested locations.”

Godaddy DNS to AWS EC2

Need help with my web server configuration. I'm using Python Django, Gunicorn, Nginx. DNS Lookup tool shows correct IP. When I go to my domain name it returns Bad Request (400)
#GODADDY RECORDS:
Type Name Value TTL Actions
1. a # 11.222.33.444 600 seconds
2. a www 11.222.33.444 600 seconds
#My SETTINGS.PY FILE:
ALLOWED_HOSTS = ['11.222.33.444']
#NGNIX SETTINGS:
server {
listen 80;
server_name 11.222.33.444 domainname.com www.domainname.com;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/ubuntu/Portfolio;
}
location / {
include proxy_params;
proxy_pass http://unix:/home/ubuntu/Portfolio/main.sock;
}
}

Nginx merge two locations

I've split up most of my config files so it looks like this (simplified)
example.conf
server {
listen 80;
server_name example.com;
root /var/www/example.com/public_html;
#stuff......
#########
# Base WordPress configuration, setup php and everything
include /etc/nginx/snippets/wordpress.conf;
}
wordpress.conf
#stuff......
location / {
try_files /wp-content/cache/supercache/$http_host/$cache_uri/index-https.html /wp-content/cache/supercache/$http_host/$cache_uri/index.html $uri $uri/ /index.php ;
}
#stuff......
So far so good. Now I want to secure only example.com with http-auth
So I add to the example.conf the following;
location / {
auth_basic "Restricted Content";
auth_basic_user_file /etc/nginx/.htpasswd;
}
But then I get the error;
nginx: [emerg] duplicate location "/" in /etc/nginx/snippets/wordpress.conf:53
nginx: configuration file /etc/nginx/nginx.conf test failed
What is the best way to enable http-auth on this domain only?
I could create an alternative wordpress.conf but that would defeat the idea of splitting up configs.

django /admin not found

I try to setup Nginx+Gunicorn and when I go by my URL the Nginx redirects request to my app and handles it by itsels for static resource (static folder). Below my Nginx domain config:
server {
listen 80;
server_name asknow.local www.asknow.local;
root /home/ghostman/Projects/asknow/asknow;
location = /favicon.ico { access_log off; log_not_found off; }
location = /static/ {
root /home/ghostman/Projects/asknow/asknow;
}
location = / {
include proxy_params;
proxy_pass http://unix:/home/ghostman/Projects/asknow/asknow/asknow.sock;
}
}
Gunicorn daemon:
[Unit]
Description=gunicorn daemon
After=network.target
[Service]
User=ghostman
Group=www-data
WorkingDirectory=/home/ghostman/Projects/asknow/asknow
ExecStart=/home/ghostman/Projects/asknow/env/bin/gunicorn --access-logfile /home/ghostman/Projects/asknow/env/log/gunicorn.log --error-logfile /home/ghostman/Projects/asknow/env/log/gunicorn-error.log --workers 3 --bind unix:/home/ghostman/Projects/asknow/asknow/asknow.sock asknow.wsgi:application
[Install]
WantedBy=multi-user.target
The problem that I need to Nginx handles request by itself for static only (www.asknow.local/static) but it tries to handle other URLs too. So when I go to www.asknow.local/admin now Nginx tries to find a resource by path (my_project/admin). But if I go to www.asknow.local Nginx proxies a request to the Gunicorn. Gunicorn error log is empty, so it fails on Nginx side.
Nginx log
2017/11/01 04:27:22 [error] 13451#13451: *1 open() "/usr/share/nginx/html/static/img/search.svg" failed (2: No such file or directory), client: 127.0.0.1, server: asknow.local, request: "GET /static/img/search.svg HTTP/1.1", host: "www.asknow.local"
How to fix it?
Your issue is using =, that is use for absolute locations. You don't want that (only for favicon.ico) you want that
server {
listen 80;
server_name asknow.local www.asknow.local;
root /home/ghostman/Projects/asknow/asknow;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/ghostman/Projects/asknow/asknow;
}
location / {
include proxy_params;
proxy_pass http://unix:/home/ghostman/Projects/asknow/asknow/asknow.sock;
}
}

Nginx reverse proxy return 404

My Nginx installed and running, below is the config from /etc/nginx/nginx.conf , I want to forward all /api/* to my tomcat server, which is running on the same server at port 9100(type http://myhost:9100/api/apps works) , otherwise, serve static file under '/usr/share/nginx/html'. Now I type http://myhost/api/apps give an 404. What's the problem here?
upstream myserver {
server localhost:9100 weight=1;
}
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location ^~ /api/ {
proxy_pass http://myserver/;
}
location / {
}
}
The proxy_pass statement may optionally modify the URI before passing it upstream. See this document for details.
In this form:
location ^~ /api/ {
proxy_pass http://myserver/;
}
The URI /api/foo is passed to http://myserver/foo.
By deleting the trailing / from the proxy_pass statement:
location ^~ /api/ {
proxy_pass http://myserver;
}
The URI /api/foo is now passed to http://myserver/api/foo.

Resources