Nginx:403禁止的Nginx / 1.12.1(Ubuntu)

时间:2018-08-21 22:15:03

标签: django nginx

我以前从未配置过任何生产服务器,而是尝试配置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来提供静态功能和所有功能。

2 个答案:

答案 0 :(得分:1)

文件权限。错误的文件权限是导致“ 403 Forbidden”错误的另一原因。建议与NGINX一起使用目录的755和文件的644的标准设置。 NGINX用户还必须是文件的所有者

尝试更改您的Web目录上的权限

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

webdirectory内移动所有站点,不要将目录和文件留在根目录中。

答案 1 :(得分:1)

您是否看过这里的gunicorn文档,其中包含有关如何配置nginx的示例 https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-sql.html#boot-features-connect-to-production-database

在上游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