简体   繁体   中英

Gunicorn, nginx, django, inside of a docker container. Gunicorn successfully runs on port 80 but nginx fails

I'm trying to set up a simple blogging site that I wrote using the django framework. The website works except that it isn't serving static files. I imagine that's because nginx isn't running. However, when I configure it to run on any port other than 80 I get the following error:

nginx: [emerg] bind() to 172.17.0.1:9000 failed (99: Cannot assign requested address)

When I run it on a port that is already being used by gunicorn I get the following error:

nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)

My nginx configuration file is as follows:

upstream django {
        server 127.0.0.1:8080;
}

server {
    listen 172.17.0.1:9000;
    server_name my.broken.blog;
    index index.html;
    location = /assets/favicon.ico { access_log off; log_not_found off; }
    location /assets {
        autoindex on;
        alias /var/www/html/mysite/assets;
        }
    location / {
        autoindex on;
        uwsgi_pass unix:///run/uwsgi/django/socket;
        include /var/www/html/mysite/mysite/uwsgi_params;
        proxy_pass http://unix:/run/gunicorn.sock;
        }
}

But if I run nginx without starting guincorn it runs properly but I get a 403 forbidden error

Configure Nginx and Gunicorn in following way to make it work,

  1. Use unix socket to comminicate between nginx and gunicron rather than running running gunicorn in some port

Create a unit file for gunicorn in the following location

sudo nano /etc/systemd/system/gunicorn.service

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

[Service]
User=root
Group=nginx
WorkingDirectory=/path-to-project-folder
ExecStart=/<path-to-env>/bin/gunicorn --workers 9 --bind unix:/path-to-sockfile/<blog>.sock app.wsgi:application 

Restart=on-failure

[Install]
WantedBy=multi-user.target

Then start and enable gunicorn service It will generate a sock file in the specified path.

sudo systemctl start gunicorn 
sudo systemctl enable gunicorn

Note: Choose the suitable number of workers for gunicorn, can sepecify log files as follows

ExecStart=//bin/gunicorn --workers 9 --bind unix:/path-to-sockfile/.sock app.wsgi:application --access-logfile /var/log/gunicorn/access.log --error-logfile /var/log/error.log

  1. create a new configuration file specific to the project in /etct/nginx rather than edit the default nginx.conf

nano /etc/nginx/blog.conf

and add the following lines ( can also add the file in /etc/nginx/default.d/)

server {
            listen 80;
            server_name 172.17.0.1;  # your Ip
        
            location = /favicon.ico { access_log off; log_not_found off; }
            location /static/ {
                root /path-to-static-folder;
            }
            location /media/  {
                root /path-to-media-folder;
            }
    
        location / {
            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;
            proxy_pass http://unix:/path-to-sock-file/<blog-sock-file>.sock;
        }
    }

include the /etc/nginx/blog.conf to nignx.conf

---------
----------
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/blog.conf;   # the new conf file added
server {
    listen       80 default_server;
    listen       [::]:80 default_server;
    server_name  _;
    root         /usr/share/nginx/html;
    ------
-------

run sudo nginx -t // to check for errors in nginx configuration.
run sudo systemctl nginx restart

refer: https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-ubuntu-16-04

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM