linux – 如何在nginx中限制对动态生成位置的访问?
我的开发人员希望只允许向局域网中的用户下载一些文件.我说好的很简单,我写了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块一切正常,我可以下载文件. 对我来说这不是很奇怪,因为它唯一的简单限制……为什么在启用它之后nginx无法找到该文件… 解决方法
两个建议:
>将root指令移动到服务器块中,因此它将应用于其后的所有位置块.这似乎是你的意图. 以及其他一些反馈: 在处理位置指令时,文件是否物理存在的问题没有区别,它们无论如何都会匹配. 我看到的另一个不匹配是你的日志条目引用“restricteddir”,但你的“完整配置”帖子没有提到这一点.它提到了一个“报告”目录. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |