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添加一个上游块,如果你将来需要添加或更改它,它会更容易.

相关文章

1、安装Apache。 1)执行如下命令,安装Apache服务及其扩展包...
一、先说一下用ansible批量采集机器信息的实现办法: 1、先把...
安装配置 1. 安装vsftpd 检查是否安装了vsftpd # rpm -qa | ...
如何抑制stable_secret读取关键的“net.ipv6.conf.all.stabl...
1 删除0字节文件 find -type f -size 0 -exec rm -rf {} ...
## 步骤 1:安装必要的软件包 首先,需要确保系统已安装 `dh...