【问题标题】:Gunicorn, nginx, django, inside of a docker container. Gunicorn successfully runs on port 80 but nginx failsGunicorn,nginx,django,在 docker 容器内。 Gunicorn 在 80 端口成功运行,但 nginx 失败
【发布时间】:2022-01-05 06:12:58
【问题描述】:

我正在尝试建立一个使用 django 框架编写的简单博客站点。该网站可以正常工作,只是它不提供静态文件。我想那是因为 nginx 没有运行。但是,当我将其配置为在 80 以外的任何端口上运行时,我收到以下错误:

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

当我在 gunicorn 已使用的端口上运行它时,我收到以下错误:

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

我的nginx配置文件如下:

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

但是,如果我在没有启动 guincorn 的情况下运行 nginx,它会正常运行,但会出现 403 禁止错误。

按照建议的答案,我没有收到任何错误,但该网站返回 403 禁止,并且没有显示 gunicorn 应该提供的网站部分。

【问题讨论】:

  • 服务器 { 听 80; server_name 172.17.0.1;
  • 指定监听端口
  • 使用以下设置静态文件
  • 位置 /static/ { root /path/to/static }

标签: django nginx gunicorn


【解决方案1】:

按照以下方式配置 Nginx 和 Gunicorn 以使其工作,

  1. 使用 unix 套接字在 nginx 和 gunicron 之间进行通信,而不是在某些端口上运行 gunicorn

在以下位置为 gunicorn 创建一个单元文件

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

然后启动并启用 gunicorn 服务 它会在指定路径生成一个sock文件。

sudo systemctl enable gunicorn
sudo systemctl start gunicorn 

注意:为gunicorn选择合适的worker数量,可以如下指定日志文件

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

  1. 在 /etct/nginx 中创建一个特定于项目的新配置文件,而不是编辑默认的 nginx.conf

纳米/etc/nginx/blog.conf

并添加以下几行(也可以在/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;
        }
    }

将 /etc/nginx/blog.conf 包含到 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;
    ------
-------

运行 sudo nginx -t // 检查 nginx 配置中的错误。
运行 sudo systemctl nginx restart

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

【讨论】:

  • 我编辑了我的帖子以包含您的建议/错误。感谢您的回复!
猜你喜欢
  • 2015-03-02
  • 2015-01-07
  • 2021-09-23
  • 1970-01-01
  • 2016-11-18
  • 2013-03-25
  • 2013-03-01
  • 1970-01-01
  • 2021-10-06
相关资源
最近更新 更多