[英]Nginx: 403 Forbidden nginx/1.12.1 (Ubuntu)
我以前從未配置過任何生產服務器,而是嘗試配置nginx並不斷收到403 Forbidden錯誤。 我不知道發生這種情況的原因。
這是完整的錯誤報告:
[crit] 25145#25145: *1 connect() to unix:/home/albert/deploy_test/django_env
/run/gunicorn.sock failed (13: Permission denied) while connecting to
upstream, client: 192.168.1.118, server: 192.168.1.118, request: "GET /
HTTP/1.1", upstream: "http://unix:/home/albert/deploy_test/django_env
/run/gunicorn.sock:/", host: "192.168.1.118"
這是我的/etc/nginx/sites-available/deployproject.conf
:
(我刪除了默認配置,並創建了如下所示的符號鏈接: sudo ln -s /etc/nginx/sites-available/deployproject.conf /etc/nginx/sites-enabled/deployproject.conf
)
upstream sample_project_server {
# fail_timeout=0 means we always retry an upstream even if it failed
# to return a good HTTP response (in case the Unicorn master nukes a
# single worker for timing out).
server unix:/home/albert/deploy_test/django_env/run/gunicorn.sock fail_timeout=0;
}
server {
listen 80;
server_name 192.168.1.118;
client_max_body_size 4G;
access_log /home/albert/logs/nginx-access.log;
error_log /home/albert/logs/nginx-error.log;
location /static/ {
alias /home/albert/static/;
}
location /media/ {
alias /home/albert/media/;
}
location / {
# an HTTP header important enough to have its own Wikipedia entry:
# http://en.wikipedia.org/wiki/X-Forwarded-For
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# enable this if and only if you use HTTPS, this helps Rack
# set the proper protocol for doing redirects:
# proxy_set_header X-Forwarded-Proto https;
# pass the Host: header from the client right along so redirects
# can be set properly within the Rack application
proxy_set_header Host $http_host;
# we don't want nginx trying to do something clever with
# redirects, we set the Host: header above already.
proxy_redirect off;
# set "proxy_buffering off" *only* for Rainbows! when doing
# Comet/long-poll stuff. It's also safe to set if you're
# using only serving fast clients with Unicorn + nginx.
# Otherwise you _want_ nginx to buffer responses to slow
# clients, really.
# proxy_buffering off;
# Try to serve static files from nginx, no point in making an
# *application* server like Unicorn/Rainbows! serve static files.
if (!-f $request_filename) {
proxy_pass http://sample_project_server;
break;
}
}
# Error pages
error_page 500 502 503 504 /500.html;
location = /500.html {
root /home/albert/static/;
}
}
這是我用來部署應用程序的完整教程。 在這里,我只是嘗試部署最原始的默認django應用程序,但是在我的真實應用程序中,我將django用作服務器端,因此似乎不需要nginx來提供靜態等服務。
文件權限。 錯誤的文件權限是導致“ 403 Forbidden”錯誤的另一原因。 建議與NGINX一起使用目錄的755和文件的644的標准設置。 NGINX用戶還必須是文件的所有者
嘗試更改您的Web目錄上的權限
sudo chown -R albert:www-data /webdirectory
sudo chmod -R 0755 /webdirectory
將您的所有站點移動到webdirectory
目錄中,請勿將目錄和文件保留在根目錄中。
您是否看過這里的gunicorn文檔,其中包含有關如何配置nginx的示例http://docs.gunicorn.org/en/stable/deploy.html
您可以嘗試在上游sample_project_server中通過TCP代替unix套接字運行gunicorn嗎,將服務器替換為: server 192.168.0.7:8000 fail_timeout=0;
gunicorn中的設置是什么? 您可以使用以下命令通過TCP綁定到localhost,以檢查unix套接字是否存在問題:-- --bind 127.0.0.1:8000
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.