微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

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

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

server {
        listen       80;
        server_name  localhost;

        location / {
            root   html;
            index  index.html index.htm index.PHP;
        }

        location ~ \.PHP${
            root           html;
            fastcgi_pass   unix:/var/run/PHP-fpm/PHP-fpm.sock;
            fastcgi_index  index.PHP;
            fastcgi_param  SCRIPT_FILENAME /srv/http/Nginx/$fastcgi_script_name;
            include        fastcgi_params;
        }
}

我该怎么办?非常感谢!

解决方法

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

server {
  server_name "" 1.2.3.4;

  root /path/to/root;
  index index.PHP;

  include fastcgi_params;
  fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

  # deny everything that doesn't match another location
  location / { deny all; }

  # allow loading /index.PHP
  location = / { } # need to allow GET / to internally redirect to /index.PHP
  location = /index.PHP { fastcgi_pass _PHP; }

  # allow access to PHPmyadmin
  location /PHPmyadmin/ { } # Allow access to static files in /PHPmyadmin/
  location ~ ^/PHPmyadmin/.*\.PHP${ fastcgi_pass _PHP; } # PHPmyadmin PHP files
}

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

原文地址:https://www.jb51.cc/linux/396180.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐