我正在尝试在我的网站上分享一些我的个人媒体,但我收到了403 禁止当我尝试访问它时出现错误(www.example.com/media)。但是,当我提供文件的完整路径(例如 www.example.com/media/2001/golden_retriever.jpg)时,我可以看到实际的图片。
我已autoindex on;
在 /etc/nginx/sites-available/example.com 中启用此功能,如下所示:
location /media {
allow all;
autoindex on;
}
在我使用 重新启动后service nginx restart
,我得到了同样的错误。我尝试将 放入autoindex on;
和location / { }
实际server { }
块内,但没有成功。基本上,当我在更改后重新启动 nginx 服务器时什么都没有发生。
整个站点可用的配置:
server {
listen 80; ## listen for ipv4; this line is default and implied
#listen [::]:80 default ipv6only=on; ## listen for ipv6
root /usr/share/nginx/www/example;
index index.php index.html index.htm;
# Make site accessible from http://localhost/
server_name example.com www.example.com;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to index.html
try_files $uri $uri/ /index.html;
# Uncomment to enable naxsi on this location
# include /etc/nginx/naxsi.rules
}
location /doc/ {
alias /usr/share/doc/;
autoindex on;
allow 127.0.0.1;
deny all;
}
location /media {
allow all;
autoindex on;
}
# Only for nginx-naxsi : process denied requests
#location /RequestDenied {
# For example, return an error code
#return 418;
#}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
#error_page 500 502 503 504 /50x.html;
#location = /50x.html {
# root /usr/share/nginx/www;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
# NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
# With php5-cgi alone:
#fastcgi_pass unix:/var/run/php5-fpm.sock;
# With php5-fpm:
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
请注意,此配置具有指向具有完全相同名称的 sites-enabled 的符号链接。
答案1
看起来问题出在符号链接上。创建符号链接时,使用完整路径代替相对路径像这样:
ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enable/example.com
答案2
您是否确定该目录/usr/share/nginx/www/example/media
已设置权限,以便 Web 服务器进程可以访问该目录?
如果您通过自己的用户帐户创建了目录,则需要确保其他权限组对该目录具有读取和执行权限。
chmod a+rx /usr/share/nginx/www/example/media
为目录添加读/执行权限。
此外,目录中的文件应该具有其他组的读取权限:
chmod a+r /usr/share/nginx/www/example/media/*
- 泰罗