简体   繁体   中英

403 Forbidden: directory index of "/path/to/files" is forbidden Nginx

I want to be really careful here because I just had to destroy my entire server and build again from scratch (total nightmare) because I tried fixing this error following advice such as

sudo chown -R user:user *
sudo chmod 755 [directory name]
sudo chmod 644 *

and ended up severely screwing up my permissions and breaking the whole Ubuntu system.

I've also followed the advice from other similar questions to take out the second $uri from my Nginx config and that has not fixed the problem.

My user1 has root privileges given by usermod -aG sudo user1

My Nginx error log says

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

And my domain name shows

403 Forbidden

ps -ef | grep nginx ps -ef | grep nginx returns

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

My nginx config

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;
    }
}

I'd suggest using an application server to facilitate communication between Django and Nginx.

You can use something like uWSGI.

Here's the link to official documentation -

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

Or if you want an easier version -

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

There are other options as well, for eg gunicorn - https://tutos.readthedocs.io/en/latest/source/ndg.html

First, I had to add this to my Nginx config which I found out via another question I asked.

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";
}

...
}

At this point, I was seeing a 404 Error . I solved that by changing try_files $uri =404; to try_files $uri $uri/ =404; however that caused a 403 Forbidden to occur on all pages except for the home page. The key was to take out try_files; altogether and the application now works perfectly.

I also set up Supervisord by following the Channels Docs to run Daphne continuously.

please try uncomment the autoindex on; directive in location block.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM