linux – 如何在nginx中限制对动态生成位置的访问?

前端之家收集整理的这篇文章主要介绍了linux – 如何在nginx中限制对动态生成位置的访问?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我的开发人员希望只允许向局域网中的用户下载一些文件.我说好的很简单,我写了Nginx配置的更改,如下所示:

  1. location /restricteddir/download/file {
  2. allow 192.168.0.0/16;
  3. allow 10.0.0.0/8;
  4. deny all;
  5. }

好吧从外面我得到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
这是此虚拟主机的完整配置:

  1. #example.com:443
  2. server {
  3. listen 443 default;
  4. ssl on;
  5. ssl_certificate example.crt;
  6. ssl_certificate_key example.key;
  7. ssl_session_timeout 15m;
  8. ssl_protocols SSLv2 SSLv3 TLSv1;
  9. ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
  10. ssl_prefer_server_ciphers on;
  11. server_name example.com;
  12. access_log /var/log/Nginx/example.com.access.log;
  13. error_log /var/log/Nginx/example.com.error.log;
  14. location / {
  15. root /sites/public;
  16. index index.PHP;
  17. if (!-f $request_filename) {
  18. rewrite ^.*$/index.PHP last;
  19. break;
  20. }
  21. }
  22. location /restricteddir/download/file {
  23. allow 192.168.0.0/16;
  24. allow 10.0.0.0/8;
  25. deny all;
  26. }
  27. #PHP
  28. location ~ \.PHP${
  29. fastcgi_pass 127.0.0.1:9000;
  30. fastcgi_index index.PHP;
  31. include /fastcgi_params;
  32. fastcgi_param SCRIPT_FILENAME /sites/public$fastcgi_script_name;
  33. }
  34. }
  35. server {
  36. listen 80 default;
  37. server_name example.com;
  38. access_log off;
  39. rewrite ^(.*)$https://example.com$1 permanent;
  40. }

我在日志中创建了这个:

  1. 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

所以也许我应该这样写:

  1. location /sites/public/restricteddir/download/file {
  2. allow 192.168.0.0/16;
  3. allow 10.0.0.0/8;
  4. deny all;
  5. }

EDIT2

我将root指令移动到服务器块后现在在日志中启用了对location / restricteddir / download / file的限制后我有:

  1. 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配置的方式:

  1. #example.com:443
  2. server {
  3. listen 443 default;
  4. ssl on;
  5. ssl_certificate example.crt;
  6. ssl_certificate_key example.key;
  7. ssl_session_timeout 15m;
  8. ssl_protocols SSLv2 SSLv3 TLSv1;
  9. ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
  10. ssl_prefer_server_ciphers on;
  11. server_name example.com;
  12. access_log /var/log/Nginx/example.com.access.log;
  13. error_log /var/log/Nginx/example.com.error.log;
  14. root /sites/public;
  15. location / {
  16. index index.PHP;
  17. if (!-f $request_filename) {
  18. rewrite ^.*$/index.PHP last;
  19. break;
  20. }
  21. }
  22. location /restricteddir/download/file {
  23. allow 192.168.0.0/16;
  24. allow 10.0.0.0/8;
  25. deny all;
  26. }
  27. #PHP
  28. location ~ \.PHP${
  29. fastcgi_pass 127.0.0.1:9000;
  30. fastcgi_index index.PHP;
  31. include /fastcgi_params;
  32. fastcgi_param SCRIPT_FILENAME /sites/public$fastcgi_script_name;
  33. }
  34. }
  35. server {
  36. listen 80 default;
  37. server_name example.com;
  38. access_log off;
  39. rewrite ^(.*)$https://example.com$1 permanent;
  40. }

所以我只是将root指令移动到服务器块,之后Nginx看起来很好,但仍然在设置404.我很困惑,因为我评论出位置/ restricteddir / download / file块一切正常,我可以下载文件.

对我来说这不是很奇怪,因为它唯一的简单限制……为什么在启用它之后Nginx无法找到该文件

最佳答案
两个建议:

>将root指令移动到服务器块中,因此它将应用于其后的所有位置块.这似乎是你的意图.
>请记住,location directive适用于URI而非*文件路径.因此,例如,location / sites / public的想法没有意义.

以及其他一些反馈:

在处理位置指令时,文件是否物理存在的问题没有区别,它们无论如何都会匹配.

我看到的另一个不匹配是你的日志条目引用“restricteddir”,但你的“完整配置”帖子没有提到这一点.它提到了一个“报告”目录.

猜你在找的Nginx相关文章