标题说了这一切,但我有一个非常有趣的问题,我很困惑。 基本上,我有一个简单的WordPress安装,我想拒绝一个文件夹(连同文件夹中的所有文件),让我们说/ wp-content / themes / default / scripts,但允许某些IP。
我可以拒绝这个文件夹的位置^~/wp-content/themes/default/scripts {deny all;}就像任何Nginx的大师会告诉你的一样。
但从我的理解,“^”具有更高的优先级,一旦find正则expression式匹配,将停止search其他位置块。 因为我不想拒绝每个人的文件夹(当然,使用allow (IP Address); ;,我的^~/wp-content/...位置块完全wh出我的PHP-FPM位置块来通过文件到FastCGI服务器。当然,当我试图查看文件夹中的任何文件时,PHP文件是直接下载,因为PHP不parsing它。
有没有人有一个想法? 我想阻止该文件夹,但为了让PHP同时为我决定允许的用户工作。 这是一个相当棘手的问题。 我找不到任何问题的答案。
多谢你们! 真的很感谢你的帮助!
对于任何人想知道,我现在的Nginx的vhostconfiguration如下所示:
server { #..all of the common stuff you would expect include /folder/nginx.conf; #including some WordPress rewrites for W3 Total Cache # pass the PHP scripts to FastCGI server listening on UNIX socket location ~ \.php$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include /etc/nginx/fastcgi_params; fastcgi_read_timeout 1900; } location / { #allowing some IPs... #deny everyone else deny all; #WordPress rewrite for slugs index index.php; if (!-e $request_filename) { rewrite ^(.*)$ /index.php last; } #rewrite for sitemaps rewrite ^(.*/)?sitemap.xml /wp-content/sitemap.php last; } # denying access to .htaccess files location ~ /\.ht { deny all; } }
终于find了答案。 当你这样做时,你需要重新声明PHP-FPM设置( location ~ \.php$ { (this code) }块中的所有东西。
所以为了避免冗余,我把这些值放在另一个文件中,留下了这样的内容:
server { # pass the PHP scripts to FastCGI server listening on UNIX socket location ~ \.php$ { include /etc/nginx/fastcgi_php_text; } location / { index index.php; } location ^~/folder/ { deny all; allow ...; include /etc/nginx/fastcgi_php_text; } }
不知道这是否是最好的办法,但这是我想出的唯一方法。
您也可以尝试通过@name定义一个位置块并引用它。 从nginx陷阱页面
server { location ~ \.php$ { try_files @fascgi; } location ^~ /folder { deny all; allow 127.0.0.1; location ~ \.php$ { try_files @fascgi; } } location @fascgi { fastcgi_pass 127.0.0.1:9000; include fastcgi_params; } }