linux – 设置nginx.conf以拒绝除某些文件或目录之外的所有连接

前端之家收集整理的这篇文章主要介绍了linux – 设置nginx.conf以拒绝除某些文件或目录之外的所有连接前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我正在尝试设置Nginx,以便拒绝所有与我的数字ip的连接,除了一些任意目录和文件.因此,如果有人访问我的IP,他们可以访问index.PHP文件PHPmyadmin目录,但是如果他们尝试访问任何其他目录,他们将被拒绝.

这是我在Nginx.conf中的服务器块:

  1. server {
  2. listen 80;
  3. server_name localhost;
  4. location / {
  5. root html;
  6. index index.html index.htm index.PHP;
  7. }
  8. location ~ \.PHP${
  9. root html;
  10. fastcgi_pass unix:/var/run/PHP-fpm/PHP-fpm.sock;
  11. fastcgi_index index.PHP;
  12. fastcgi_param SCRIPT_FILENAME /srv/http/Nginx/$fastcgi_script_name;
  13. include fastcgi_params;
  14. }
  15. }

我该怎么办?非常感谢!

最佳答案
最简单的方法是首先拒绝所有访问,然后只授予对所需目录的访问权限.正如ring0指出的那样,你可以使用listen指令的默认值(default_server in 0.8)标志.但是,如果您已经有一台服务器要用作主机未知命名访问的默认服务器,您也可以只捕获没有主机头的请求或服务器的ip地址,如下所示(用你的1.2.3.4替换)服务器的IP:

  1. upstream _PHP {
  2. server unix:/var/run/PHP-fpm/PHP-fpm.sock;
  3. }
  4. server {
  5. server_name "" 1.2.3.4;
  6. root /path/to/root;
  7. index index.PHP;
  8. include fastcgi_params;
  9. fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  10. # deny everything that doesn't match another location
  11. location / { deny all; }
  12. # allow loading /index.PHP
  13. location = / { } # need to allow GET / to internally redirect to /index.PHP
  14. location = /index.PHP { fastcgi_pass _PHP; }
  15. # allow access to PHPmyadmin
  16. location /PHPmyadmin/ { } # Allow access to static files in /PHPmyadmin/
  17. location ~ ^/PHPmyadmin/.*\.PHP${ fastcgi_pass _PHP; } # PHPmyadmin PHP files
  18. }

fastcgi_params将由fastcgi_pass和仅允许/index.PHP和/ PHPmyadmin /的两个位置继承.我还为PHP添加了一个上游块,如果你将来需要添加或更改它,它会更容易.

猜你在找的Nginx相关文章