skip to Main Content

I’m trying for the first time to deploy my Django application on a server but so far I wasn’t able to get rid of port in my URL. Right now I’m using Gunicorn with Nginx with the following configuration.

Nginx /etc/nginx/sites-enabled/site.conf

server {
       listen 8000;
       server_name example.com;
       location = /favicon.ico {access_log off;log_not_found off;}

       location /static/ {
         root /home/webapp/um;
       }
       location /media/ {
         root /home/webapp/um;
       }

       location / {
         include proxy_params;
         proxy_pass http://unix:/home/webapp/um/um.sock;
       }
     }

/etc/nginx/proxy_params

proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;

Gunicorn /etc/systemd/system/gunicorn.service

Description=gunicorn service
After=network.target

[Service]
User=root
Group=www-data
WorkingDirectory=/home/webapp/um/
ExecStart=/root/um/bin/gunicorn --access-logfile - --workers 3 --bind unix:/home/webapp/um/um.sock um.wsgi:application

[Install]
WantedBy=multi-user.target

Gunicorn binding

gunicorn --bind 0.0.0.0:8000 um.wsgi:application

Changing port 8000 with port 80 in /etc/nginx/sites-enabled/site.conf gives me a 404 on nginx. Using port 8000 I’m able to see the site using http://example.com:8000/myapp but I’m aiming at using http://example.com/myapp as my address.
As a side note, the VPS I’m installing the app on came with Plesk already installed with which I’m also not familiar with. I don’t know if Plesk might be interferring in catching traffic from port 80.

Thanks in advance

2

Answers


  1. Chosen as BEST ANSWER

    After a bit of struggling, I found the solution. Turns out my config was correct, but there was an nginx config file automatically written by plesk that was catching requests on port 80. The content of such file is

    server {
        listen 111.111.111.111:80;
    
        location ^~ /plesk-site-preview/ {
                proxy_pass http://127.0.0.1:8880;
                proxy_set_header Host             plesk-site-preview.local;
                proxy_set_header X-Real-IP        $remote_addr;
                proxy_set_header X-Forwarded-For  $proxy_add_x_forwarded_for;
                proxy_cookie_domain plesk-site-preview.local $host;
                access_log off;
        }
    
        location / {
                proxy_pass http://111.111.111.111:7080;
                proxy_set_header Host $host;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
    }
    
    server {
        listen 111.111.111.111:443 ssl;
    
        ssl_certificate             /opt/psa/var/certificates/certWpPLaPv;
        ssl_certificate_key         /opt/psa/var/certificates/certWpPLaPv;
    
        location ^~ /plesk-site-preview/ {
                proxy_pass http://127.0.0.1:8880;
                proxy_set_header Host             plesk-site-preview.local;
                proxy_set_header X-Real-IP        $remote_addr;
                proxy_set_header X-Forwarded-For  $proxy_add_x_forwarded_for;
                proxy_cookie_domain plesk-site-preview.local $host;
                access_log off;
        }
    
        location / {
                proxy_pass https://111.111.111.111:7081;
                proxy_set_header Host $host;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
    

    Once I removed that file from inclusion in nginx.conf everything started working. As a suggestion for whoever is facing a similar situation, I would recommend to check what Nginx is processing using the following command

    nginx -T
    

    1. You just need to listen this server on port 80 instead of 8000
      save gunicorn as described
    server {
           listen 80;
           server_name 52.14.64.58 example.com www.example.com;
           location = /favicon.ico {access_log off;log_not_found off;}
    
           location /static/ {
             root /home/webapp/um;
           }
           location /media/ {
             root /home/webapp/um;
           }
    
           location / {
             include proxy_params;
             proxy_pass http://unix:/home/webapp/um/um.sock;
           }
         }
    

    52.14.64.58 => is ipv4 of your virtual machine, it could be anything in your case.

    1. Now time to make changes in our django settings
    ALLOWED_HOSTS = ['IP_ADDRESS', 'example.com', 'www.example.com']
    
    1. Now check nginx status then restart gunicorn and nginx . I hope it would work for you.
    sudo nginx -t
    sudo systemctl restart nginx
    sudo systemctl restart gunicorn
    
    1. Now setup your domain by it’s dns settings.
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search