禁止使用nginx目录索引(403错误)

禁止使用nginx目录索引(403错误),nginx,http-status-code-403,Nginx,Http Status Code 403,在node.js应用程序中,我遇到了一个奇怪的问题,即所有媒体文件都从nginx获得403错误。 文件结构如下所示: /www/ └── app ├── assets │   ├── fonts │   │   └── bootstrap │   ├── images │   │   ├── ios │   │   └── photoswipe │   ├── js │   └── vendor-css └── public

在node.js应用程序中,我遇到了一个奇怪的问题,即所有媒体文件都从nginx获得
403
错误。 文件结构如下所示:

/www/
└── app
    ├── assets
    │   ├── fonts
    │   │   └── bootstrap
    │   ├── images
    │   │   ├── ios
    │   │   └── photoswipe
    │   ├── js
    │   └── vendor-css
    └── public  <-----------(here is the problem)
        └── files
            ├── attachments
            │   ├── thumbnails
            │   └── thumbnails2
            └── profilepics
我已将
777
设置为
/www
的文件权限(我知道这不是一个好做法,只是为了测试):

nginx错误日志示例:

[错误]30102#30102:*34“/www/app/public/files”的目录索引为 禁止,客户端:1.2.3.4,服务器:example.com,请求:“GET” /media/attachments/mycat.jpg/HTTP/1.1“,主机:“example.com”, 推荐人:“

请注意,nginx会自动添加尾部斜杠

当我尝试使用curl获取
mycat.jpg
(不带尾随斜杠)时,我得到:

301永久搬迁

其他路径上的文件,即
/www/app/assets
正确呈现


我已经移动到了
/public/files
的路径,并使用了nginx-config,但这个问题仍然困扰了我好几个小时。非常感谢您帮助解决此问题。

成为root用户应该可以解决此问题。您是否在尝试访问图像“mycat.jpg”时使用root?您的访问规则是什么?您可以使用“chmod”更改这些规则。

嗯,这些文件位于远程服务器上,我试图从客户端获取文件。我是服务器的root用户,可以更改任何权限。@Karlom您当前的权限是什么?
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;

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