This is how my /etc/nginx/nginx.conf
file looks like (as per nginx -T
):
# configuration file /etc/nginx/nginx.conf:
# For more information on configuration, see:
# * Official English Documentation: http://nginx.org/en/docs/
# * Official Russian Documentation: http://nginx.org/ru/docs/
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
# Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;
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 / {
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
}
This are the contents of /etc/nginx/conf.d/sv1.conf
:
upstream sv1 {
server unix:/var/www/app/app1/app1.sock;
}
server {
listen 80;
location /app1 {
uwsgi_pass sv1;
include /var/www/app/app1/uwsgi_params;
}
location /app1-static {
alias /var/www/app/app1/static/;
}
location /app1-media {
alias /var/www/app/app1/media/;
}
}
This are the contents of /etc/nginx/conf.d/sv2.conf
:
upstream sv2 {
server unix:/var/www/app/app2/app2.sock;
}
server {
listen 80;
location /app2 {
uwsgi_pass sv2;
include /var/www/app/app2/uwsgi_params;
}
location /app2-static {
alias /var/www/app/app2/static/;
}
location /app2-media {
alias /var/www/app/app2/media/;
}
}
However, when I access any of my apps on either <my-ip-address>/app1
or <my-ip-address>/app2
, I get a 404 error and the logs say the following:
"/usr/share/nginx/html/app1/index.html" is not found (2: No such file or directory)
It is apparently looking for an app1/index.html
file under the /usr/share/nginx/html
directory defined by the first server's root
directive, despite both the other server's location directives being better matches for the requested url, according to this digitalocean.com community tutorial:
When trying to determine which server block to send a request to, Nginx will first try to decide based on the specificity of the listen directive [...] It is important to understand that Nginx will only evaluate the server_name directive when it needs to distinguish between server blocks that match to the same level of specificity in the listen directive
According to uWSGI everything is working OK and both (Django) projects run perfectly fine independently under their own debug servers (via manage.py runserver
)
Why is NGINX ignoring my upstream directives? Furthermore, they work fine if I just include the listen directives under the default server block, but I NEED them on their own server blocks because I have to to redirect different subdomains to each app.