最简单的方法是首先拒绝所有访问,然后只授予对所需目录的访问权限.正如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添加了一个上游块,如果你将来需要添加或更改它,它会更容易.