1

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.

arielnmz
  • 433
  • 1
  • 4
  • 13

1 Answers1

4

You have no server_name specified in either of your virtual hosts. Therefore nginx selects the virtual host which is the default, specified with the listen 80 default_server directive in the main nginx configuration.

You need to specify the virtual host like this:

upstream sv1 {
    server unix:/var/www/app/app1/app1.sock;
}

server {
    listen 80;
    server_name app1.example.com;

    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/;
    }
}

And then you need to access the application with the domain name. If you don't have a DNS entry for the domain name set up, then you need to edit your /etc/hosts or C:\Windows\SYSTEM32\drivers\etc\hosts file on the computer you use to access the application.

Tero Kilkanen
  • 36,796
  • 3
  • 41
  • 63
  • 1
    Thank you so much, I still have a small problem, though, since I host both apps on the same server I have the problem that only `app1` will answer to the requests because they both share the same `server_name`, despite having a different `location`, how can I go about this withouth having to use different `server_name`s? (the server name is the IP of my VPS, though, that's why it's the same for both, for now) – arielnmz Sep 30 '17 at 14:54
  • 1
    If you only have one `server_name`, then you need to include all `location` blocks under that. However, it is best to use different names in the start, if you plan on having different virtual servers for both applications. – Tero Kilkanen Oct 01 '17 at 13:31