jarjar
By:
jarjar

stuck on 'welcome to nginx' gunicorn django ubuntu 16.04

July 12, 2016 3.4k views
Nginx Deployment Django Ubuntu 16.04

I am stuck on the 'welcome to nginx' page and I need help. Here is what I have and tried.

Test passed:
gunicorn --bind 0.0.0.0:8000 myapp.wsgi:application

/etc/systemd/system/gunicorn.service:

[Unit]
Description=gunicorn daemon
After=network.target

[Service]
User=jarjar
Group=www-data
WorkingDirectory=/home/jarjar/myapp
ExecStart=/home/jarjar/myapp/myappenv/bin/gunicorn --workers 3 --bind unix:/home/jarjar/myapp/myapp.sock myapp.wsgi:application

[Install]
WantedBy=multi-user.target

/etc/nginx/sites-available/myapp:


server {
    listen 80 default_server;
    listen [::]:80 default_server;
    server_name myapp.com www.myapp.com;
    return 301 https://$server_name$request_uri;

    root /home/jarjar/myapp;

    location = /favicon.ico { access_log off; log_not_found off; }
    location /static/ {
        root /home/jarjar/myapp;
    }

    location / {
        include proxy_params;
        proxy_pass http://unix:/home/jarjar/myapp/myapp.sock;
    }

    location ~ /.well-known {
        allow all;
    }
}


server {
    listen 443 ssl http2 default_server ;
    listen [::]:443 ssl http2 default_server;
    include snippets/ssl-myapp.com.conf;
    include snippets/ssl-params.conf;
}

sudo rm /etc/nginx/sites-available/default
sudo systemctl start gunicorn
sudo systemctl enable gunicorn
sudo nginx -t
sudo systemctl restart nginx

5 comments
  • What does the following command output:

    ls -l /etc/nginx/sites-enabled
    
  • @gndo
    total 0
    lrwxrwxrwx 1 root root 38 Jul 11 09:26 myapp -> /etc/nginx/sites-available/myapp

    Also, my nginx error.log is giving me a
    28 [error] 28452#28452: send() failed (111: Connection refused) while resolving, resolver: xx.xx.xxx.xxx:53

  • @jarjar - I'd double check your URL. That doesn't look correct

        location / {
            include proxy_params;
            ## What does it mean to have both http and unix protocols
            proxy_pass http://unix:/home/jarjar/myapp/myapp.sock;
        }
    
  • @gndo

    # If I remove 'http://' I get:
    nginx: [emerg] invalid URL prefix in /etc/nginx/sites-enabled/myapp:16
    nginx: configuration file /etc/nginx/nginx.conf test failed
    
    # If I remove 'unix:/' I get:
    nginx: [emerg] host not found in upstream "home" in /etc/nginx/sites-available/myapp
    nginx: configuration file /etc/nginx/nginx.conf test failed
    

    I followed this tutorial when I set up my server. He said to put:

        location / {
            include proxy_params;
            proxy_pass http://unix:/home/sammy/myproject/myproject.sock;
        }
    
    Django is a powerful web framework that can help you get your Python application or website off the ground. Django includes a simplified development server for testing your code locally, but for anything even slightly production related, a more secure and powerful web server...
  • @jarjar - OK, I've seen the socket surrounded with colons, but maybe the parser allows dropping ending part? From the nginx documentation:

    
    Syntax: proxy_pass URL;
    Default:    —
    Context:    location, if in location, limit_except
    Sets the protocol and address of a proxied server and an optional URI to which a location should be mapped. As a protocol, “http” or “https” can be specified. The address can be specified as a
    ```code```domain name or IP address, and an optional port:
            proxy_pass http://localhost:8000/uri/;
    or as a UNIX-domain socket path specified after the word “unix” and enclosed in colons:
            proxy_pass http://unix:/tmp/backend.socket:/uri/;
    
    
    
2 Answers
AlexeiRamone July 12, 2016
Accepted Answer

This will never work because you're redirecting http to https (server block with listen on 443) and there's no location there (except if inside those includes)

If you're running this site via http, remove the return 301 ...
If you plan on serving it only in https, remove all location entries from the server entry that listens to 80 and put them inside the server block that listens to 443.

  • @AlexeiRamone I modified my /etc/nginx/sites-available/myapp, however now I'm getting a 502 Bad Gateway error.

    2016/07/12 14:00:59 [error] 29035#29035: *1 upstream prematurely closed connection while reading response header from upstream, client: xx.xxx.xxx.xxx, server: , request: "GET / HTTP/2.0", upstream: "http://unix:/home/jarjar/myapp/myapp.sock:/", host: "www.myapp.com"
    

    Updated /etc/nginx/sites-available/myapp

    server {
        listen 80;
        listen [::]:80;
        server_name myapp.com www.myapp.com;
        return 301 https://$server_name$request_uri;
    }
    
    
    server {
        listen 443 ssl http2 default_server;
        listen [::]:443 ssl http2 default_server;
        include snippets/ssl-myapp.com.conf;
        include snippets/ssl-params.conf;
    
        root /home/jarjar/myapp;
    
        location = /favicon.ico { access_log off; log_not_found off; }
        location /static/ {
            root /home/jarjar/myapp;
        }
    
        location / {
            include proxy_params;
            proxy_pass http://unix:/home/jarjar/myapp/myapp.sock;
        }
    
        location ~ /.well-known {
            allow all;
        }
    }
    
    

Try this:

rename your proxy_pass:

proxy_pass http://django_projectname_server;

and then, BEFORE your server entry

upstream django_projectname_server {
  server unix:/fullpath/to/sockfile.sock fail_timeout=0;
}
  • @AlexeiRamone It turns out I don't have to do this. The server started working all of a sudden with your prior suggestion. Thank you for your help!

    • Awesome! It works in both ways, the latter is recommended because you actually declare a upstream server and later you can add/configure other stuff like cache and balancing, but for a simple configuration we're fine by now.

Have another answer? Share your knowledge.