我正在嘗試建立一個使用 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 應該提供的網站部分。
uj5u.com熱心網友回復:
按照以下方式配置 Nginx 和 Gunicorn 以使其作業,
- 使用 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數量,可以指定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
- 在 /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 重啟
參考:https : //www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-ubuntu-16-04
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/406921.html
標籤:
