简体   繁体   中英

nginx directory index is forbidden (403 error)

In a node.js app, I have this strange problem that all media files get 403 error from nginx. the files structure is this:

/www/
└── app
    ├── assets
    │   ├── fonts
    │   │   └── bootstrap
    │   ├── images
    │   │   ├── ios
    │   │   └── photoswipe
    │   ├── js
    │   └── vendor-css
    └── public  <-----------(here is the problem)
        └── files
            ├── attachments
            │   ├── thumbnails
            │   └── thumbnails2
            └── profilepics

And here is my nginx config

server {
  listen example.com:80 default_server;
  server_name example.com;
  root /www/app;

  location ~ ^/media/ {
   alias /www/app/public/files;
  }


  location / {
   root /www/app;
  }

 location ~ ^/assets/ {
   root /www/app;
  }

  location ~ ^.+\..+$ {
    try_files $uri =404;
  }

}


server {
  listen api.example.com:80;
  server_name api.example.com;
  location / {
   proxy_pass http://localhost:3000;
   proxy_http_version 1.1;
   proxy_set_header X-Forwarded-Proto https;
   proxy_set_header Upgrade $http_upgrade;
   proxy_set_header Connection 'upgrade';
   proxy_set_header Host $host;
   proxy_cache_bypass $http_upgrade;

  }
}

I have set 777 as File permission for /www (I know it is not good practice but just for test):

# ls -al /www
total 6212
drwxrwxrwx  3 www-data www-data    4096 May  4 03:26 .
drwxr-xr-x 24 root     root        4096 May  4 00:24 ..
drwxrwxrwx  4 www-data www-data    4096 May  5 16:08 app



ls -al /www/app/public/files
total 16
drwxrwxrwx 4 www-data www-data 4096 May  5 15:59 .
drwxrwxrwx 3 www-data www-data 4096 May  5 15:59 ..
drwxrwxrwx 4 www-data www-data 4096 May  5 16:10 attachments
drwxrwxrwx 2 www-data www-data 4096 May  5 15:59 profilepics

Example nginx error log:

[error] 30102#30102: *34 directory index of "/www/app/public/files" is forbidden, client: 1.2.3.4, server: example.com, request: "GET /media/attachments/mycat.jpg/ HTTP/1.1", host: "example.com", referrer: " http://example.com/ "

Notice that nginx adds a trailing slash automatically.

When I try to get mycat.jpg (without trailing slash) using curl, I get:

301 Moved Permanently

Files on other path, ie /www/app/assets are rendered correctly.

I have moved around the path to /public/files and twaked nginx config but this problem still bugs me for hours. Really appreciate your help to resolve this.

Being root should fix the problem. Are you root when trying to access the image "mycat.jpg"? And what are your access rules you can change them by using "chmod"

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