【问题标题】:Nginx: 403 Forbidden nginx/1.12.1 (Ubuntu)Nginx: 403 禁止 nginx/1.12.1 (Ubuntu)
【发布时间】:2019-01-28 03:48:21
【问题描述】:

我以前从未配置过任何生产服务器,我正在尝试配置 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/;
    }
}

Here 是我用来部署我的应用程序的完整教程。在这里,我只是尝试部署最原始的默认 django 应用程序,但在我的真实应用程序中,我使用 django 作为服务器端,因此似乎不需要 nginx 提供静态服务。

【问题讨论】:

    标签: django nginx


    【解决方案1】:

    文件权限。不正确的文件权限是“403 Forbidden”错误的另一个原因。 NGINX 推荐使用目录的标准设置 755 和文件的标准设置 644。 NGINX 用户也需要是文件的所有者

    尝试更改您的网络目录的权限

    sudo chown -R albert:www-data /webdirectory
    sudo chmod -R 0755 /webdirectory
    

    将您的所有网站移动到webdirectory 中,不要将目录和文件留在您的根目录中。

    【讨论】:

    • 是的,以某种方式使用权限来实现它。我改变了其中的一些,所以它到底是哪一个并不容易,但是是的。感谢您提及与 nginx 一起使用的推荐权限。
    【解决方案2】:

    你看过gunicorn docs here有如何配置nginx的例子吗 http://docs.gunicorn.org/en/stable/deploy.html

    您能否尝试通过 TCP 而不是 unix 套接字运行 gunicorn,在您的上游 sample_project_server 中将服务器替换为: server 192.168.0.7:8000 fail_timeout=0; gunicorn中的设置是什么?您可以使用以下命令通过 TCP 绑定到 localhost,以检查您的 unix 套接字是否有问题: --bind 127.0.0.1:8000

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-02
      • 2019-06-07
      • 2019-04-17
      • 2019-05-29
      • 2016-04-24
      相关资源
      最近更新 更多