[英]Gunicorn, nginx, django, inside of a docker container. Gunicorn successfully runs on port 80 but nginx fails
我正在尝试建立一个使用 django 框架编写的简单博客站点。 该网站可以正常工作,只是它不提供 static 文件。 我想那是因为 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 禁止错误
按以下方式配置 Nginx 和 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 start gunicorn
sudo systemctl enable gunicorn
注意:为gunicorn选择合适的worker数量,可以指定log文件如下
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
纳米 /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 重启
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.