403 Forbidden: Nginx 禁止“/path/to/files”目录索引

问题描述 投票:0回答:3

我在这里要非常小心,因为我只需要销毁整个服务器并从头开始重新构建(完全是噩梦),因为我尝试按照诸如

之类的建议修复此错误
sudo chown -R user:user *
sudo chmod 755 [directory name]
sudo chmod 644 *

最终严重破坏了我的权限并破坏了整个 Ubuntu 系统。

我也听从了其他类似问题的建议,从我的 Nginx 配置中删除了第二个

$uri
,但这并没有解决问题。

我的

user1
拥有
usermod -aG sudo user1

的root权限

我的 Nginx 错误日志说

2019/03/06 17:45:40 [error] *1 directory index of "/home/user1/app/src/apr/" is forbidden

我的域名显示

403 Forbidden

ps -ef | grep nginx
返回

root     15419     1  0 17:45 ?        00:00:00 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
www-data 15420 15419  0 17:45 ?        00:00:00 nginx: worker process
user1  15503 15462  0 18:00 pts/1    00:00:00 grep --color=auto nginx

我的 nginx 配置

server {
    listen 80;
    #listen [::]:80 ipv6only=on;

    server_name your.server.com;
    access_log /etc/nginx/access.log;

    root /var/www/html/someroot;

    location / {
            #autoindex on;

            # First attempt to serve request as file, then
            # as directory, then fall back to displaying a 404.
            # try_files $uri =404;

            #proxy_set_header X-Real-IP $remote_addr;
            #proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            #proxy_set_header Host $http_host;
            #proxy_set_header X-NginX-Proxy true;
            #proxy_pass http://127.0.0.1:8080/;
            #proxy_redirect off;
            #proxy_http_version 1.1;
            #proxy_set_header Upgrade $http_upgrade;
            #proxy_set_header Connection "upgrade";

            #proxy_redirect off;
            #proxy_set_header   X-Forwarded-Proto $scheme;
            #proxy_cache one;
            #proxy_cache_key sfs$request_uri$scheme;
    }

    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/some/fullchain.pem;
    # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/some/privkey.pem; 
    # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot

    if ($scheme != "https") {
        return 301 https://$host$request_uri;
    }
}
nginx server http-status-code-403 pid
3个回答
0
投票

我建议使用应用服务器来促进 Django 和 Nginx 之间的通信。

你可以使用像 uWSGI 这样的东西。

这是官方文档的链接-

https://uwsgi-docs.readthedocs.io/en/latest/tutorials/Django_and_nginx.html

或者如果你想要一个更简单的版本 -

https://www.digitalocean.com/community/tutorials/how-to-serve-django-applications-with-uwsgi-and-nginx-on-ubuntu-14-04

还有其他选择,例如 gunicorn - https://tutos.readthedocs.io/en/latest/source/ndg.html


0
投票

首先,我必须将它添加到我的 Nginx 配置中,这是我通过 another question 我问的。

upstream socket {
    ip_hash;
    server $DAPHNE_IP_ADDRESS$ fail_timeout=0;
}

server {
...

location / {
    proxy_pass http://socket;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
}

...
}

此时,我看到了一个

404 Error
。我通过将
try_files $uri =404;
更改为
try_files $uri $uri/ =404;
解决了这个问题,但是这导致 403 Forbidden 出现在除主页之外的所有页面上。关键是完全删除
try_files;
,应用程序现在可以完美运行了。

我还通过按照

Channels Docs
连续运行Supervisord来设置
Daphne


0
投票

请尝试取消注释

autoindex on;
块中的
location
指令。

© www.soinside.com 2019 - 2024. All rights reserved.