Nginx拒绝';不适用于文件夹文件
我试图限制对我的站点的访问,只允许特定的IP,但我遇到了以下问题:当我访问www.example.com时,deny可以完美地工作,但当我尝试访问www.example.com/index.php时,它返回“access denied”页面,php文件直接在浏览器中下载,无需处理。 我确实想拒绝所有IP访问网站上的所有文件,但我的IP除外。我该怎么做 以下是我的配置:Nginx拒绝';不适用于文件夹文件,nginx,Nginx,我试图限制对我的站点的访问,只允许特定的IP,但我遇到了以下问题:当我访问www.example.com时,deny可以完美地工作,但当我尝试访问www.example.com/index.php时,它返回“access denied”页面,php文件直接在浏览器中下载,无需处理。 我确实想拒绝所有IP访问网站上的所有文件,但我的IP除外。我该怎么做 以下是我的配置: server { listen 80; server_name example.com; root /var/www/exam
server {
listen 80;
server_name example.com;
root /var/www/example;
location / {
index index.html index.php; ## Allow a static html file to be shown first
try_files $uri $uri/ @handler; ## If missing pass the URI to front handler
expires 30d; ## Assume all files are cachable
allow my.public.ip;
deny all;
}
location @handler { ## Common front handler
rewrite / /index.php;
}
location ~ .php/ { ## Forward paths like /js/index.php/x.js to relevant handler
rewrite ^(.*.php)/ $1 last;
}
location ~ .php$ { ## Execute PHP scripts
if (!-e $request_filename) { rewrite / /index.php last; } ## Catch 404s that try_files miss
expires off; ## Do not cache dynamic content
fastcgi_pass 127.0.0.1:9001;
fastcgi_param HTTPS $fastcgi_https;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params; ## See /etc/nginx/fastcgi_params
}
}
这是因为您的拒绝/允许规则仅适用于一个位置 删除该选项,然后尝试:
server {
listen 80;
server_name example.com;
root /var/www/example;
if ($remote_addr != "YOUR.PUBLIC.IP") {return 403;}
...
}
由于测试在任何特定位置块之外,因此将适用于所有情况
还要注意的是,
IF
在这里不是邪恶的,因为它只是“返回”好的,所以我找到了解决方案。Nginx处理最精确的正则表达式,在本例中是php文件的正则表达式。要使配置工作,除@handler外,所有其他位置必须在/location规则中定义(不能放在任何规则下-仅作为根)
有趣的建议。谢谢现在有两个问题:1。如果我有IP列表,我应该怎么做?2.如果我只想阻止包含php文件的特定文件夹,那么最好的方法是什么?
server {
listen 80;
server_name example.com;
root /var/www/example;
location / {
index index.html index.php; ## Allow a static html file to be shown first
try_files $uri $uri/ @handler; ## If missing pass the URI to front handler
expires 30d; ## Assume all files are cachable
allow my.public.ip;
deny all;
location ~ .php/ { ## Forward paths like /js/index.php/x.js to relevant handler
rewrite ^(.*.php)/ $1 last;
}
location ~ .php$ { ## Execute PHP scripts
if (!-e $request_filename) { rewrite / /index.php last; } ## Catch 404s that try_files miss
expires off; ## Do not cache dynamic content
fastcgi_pass 127.0.0.1:9001;
fastcgi_param HTTPS $fastcgi_https;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params; ## See /etc/nginx/fastcgi_params
}
}
location @handler { ## Common front handler
rewrite / /index.php;
}
}