我的开发人员希望只允许向局域网中的用户下载一些文件.我说好的很简单,我写了Nginx配置的更改,如下所示:
location /restricteddir/download/file {
allow 192.168.0.0/16;
allow 10.0.0.0/8;
deny all;
}
好吧从外面我得到403这么好但是从内部(LAN)给我404.为什么?
我已经检查过,磁盘上不存在文件的位置.开发人员告诉我,该位置是由PHP动态生成的,因此文件位于tmp目录中,但点击链接后:
https://example.com/report/download/file/id/somefile.txt它应该开始下载,但它给出了404错误.
最后一个物理loaction是/ restricteddir / download所以file / id / somefile.txt是由PHP生成的.
为了测试我将位置更改为/ restricteddir / download,然后点击https://example.com/restricteddir/download给了我404.
禁用限制后,我很困惑,dir Everythings工作正常,我可以从https://example.com/report/download/file/id/somefile.txt下载文件并点击https://example.com/report/下载/没有404.那么bug在哪里?我应该将什么添加到我的Nginx配置中才能使其正常工作?
EDIT1
这是此虚拟主机的完整配置:
#example.com:443
server {
listen 443 default;
ssl on;
ssl_certificate example.crt;
ssl_certificate_key example.key;
ssl_session_timeout 15m;
ssl_protocols SSLv2 SSLv3 TLSv1;
ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
ssl_prefer_server_ciphers on;
server_name example.com;
access_log /var/log/Nginx/example.com.access.log;
error_log /var/log/Nginx/example.com.error.log;
location / {
root /sites/public;
index index.PHP;
if (!-f $request_filename) {
rewrite ^.*$/index.PHP last;
break;
}
}
location /restricteddir/download/file {
allow 192.168.0.0/16;
allow 10.0.0.0/8;
deny all;
}
#PHP
location ~ \.PHP${
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.PHP;
include /fastcgi_params;
fastcgi_param SCRIPT_FILENAME /sites/public$fastcgi_script_name;
}
}
server {
listen 80 default;
server_name example.com;
access_log off;
rewrite ^(.*)$https://example.com$1 permanent;
}
我在日志中创建了这个:
2012/08/07 11:55:43 [error] 11121#0: *90 open() "/usr/share/Nginx/html/restricteddir/download/file/id/somefile.txt" Failed (2: No such file or directory),client: 10.200.2.70,server: example.com,request: "GET /restricteddir/download/file/id/somefile.txt HTTP/1.1",host: "example.com"
所以现在我知道Nginx在错误的根/usr/share / Nginx / html /中找到这个位置而不是/ sites / public
所以也许我应该这样写:
location /sites/public/restricteddir/download/file {
allow 192.168.0.0/16;
allow 10.0.0.0/8;
deny all;
}
EDIT2
我将root指令移动到服务器块后现在在日志中启用了对location / restricteddir / download / file的限制后我有:
2012/08/09 10:43:12 [error] 32120#0: *71 open() "/site/public/restricteddir/download/file/id/somefile.txt" Failed (2: No such file or directory),client: 10.2.1.120,host: "example.com"
所以现在Nginx看起来正确,但没有找到enything.喜欢这个文件不是由PHP生成的?我很喜欢它,并且不知道什么是wront …这是一个简单的任务restrcit location为什么这不起作用?
EDIT3
这就是我现在查看virtualhost配置的方式:
#example.com:443
server {
listen 443 default;
ssl on;
ssl_certificate example.crt;
ssl_certificate_key example.key;
ssl_session_timeout 15m;
ssl_protocols SSLv2 SSLv3 TLSv1;
ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
ssl_prefer_server_ciphers on;
server_name example.com;
access_log /var/log/Nginx/example.com.access.log;
error_log /var/log/Nginx/example.com.error.log;
root /sites/public;
location / {
index index.PHP;
if (!-f $request_filename) {
rewrite ^.*$/index.PHP last;
break;
}
}
location /restricteddir/download/file {
allow 192.168.0.0/16;
allow 10.0.0.0/8;
deny all;
}
#PHP
location ~ \.PHP${
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.PHP;
include /fastcgi_params;
fastcgi_param SCRIPT_FILENAME /sites/public$fastcgi_script_name;
}
}
server {
listen 80 default;
server_name example.com;
access_log off;
rewrite ^(.*)$https://example.com$1 permanent;
}
所以我只是将root指令移动到服务器块,之后Nginx看起来很好,但仍然在设置404.我很困惑,因为我评论出位置/ restricteddir / download / file块一切正常,我可以下载文件.
>将root指令移动到服务器块中,因此它将应用于其后的所有位置块.这似乎是你的意图.
>请记住,location directive适用于URI而非*文件路径.因此,例如,location / sites / public的想法没有意义.
以及其他一些反馈:
在处理位置指令时,文件是否物理存在的问题没有区别,它们无论如何都会匹配.
我看到的另一个不匹配是你的日志条目引用“restricteddir”,但你的“完整配置”帖子没有提到这一点.它提到了一个“报告”目录.