nginx - 允许访问位置块 nginx 配置中的特定文件
问题描述
在我的 nginx 配置中,我已阻止来自我的 IP 的所有访问,但允许使用我的 localhost 执行此操作。我想允许对xn.php
我尝试使用的一个文件进行全局访问location ^~ /xn.php
,但它不起作用。我也尝试过location /xn.php
,但仍然失败。我该怎么做?我检查了很多文档,但我坚持下去
server {
listen 127.0.0.1:80;
root /var/www/html/;
index /index.php;
server_name localhost;
location / {
deny 77.777.77.0/24;
allow 127.0.0.1;
autoindex on;
index index.php;
try_files $uri /index.html /index.php;
deny all;
}
location ^~ /xn.php {
allow all;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
}
location ~ /\.ht {
deny all;
}
}
解决方案
您当前的配置xn.php
内容将作为 HTTP 响应发送,而不是使用 PHP-FPM 进行解释。此外,任何对 PHP 文件的请求都不会被您的deny
规则阻止,因为它与location / { ... }
阻止不匹配。你可以试试这个:
server {
listen 80;
root /var/www/html/;
index /index.php;
location / {
allow 127.0.0.1;
deny all;
autoindex on;
index index.php;
try_files $uri /index.html /index.php;
}
location = /xn.php {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
}
location ~ \.php$ {
allow 127.0.0.1;
deny all;
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
}
location ~ /\.ht {
deny all;
}
}
更新
由于您仅在127.0.0.1
接口上侦听,因此server
根本无法从任何其他主机访问此块。适合您的配置取决于server
您的 nginx 配置中的其他块。
推荐阅读
- c - 中缀到前缀和后缀的转换
- python - 有没有办法使用 tensorflow 对象检测从扫描图像中识别标题和段落?
- python - 使用python逐行比较两个csv文件中的内容
- java - java中的均衡两个列表
- python - 如何在pyspark中进行平均(目标)编码
- laravel - 如何在 Laravel Mix 中添加 Tailwind
- python - How to split output with \n properly in python?
- javascript - ESLint error: 'value' is never reassigned use 'const' instead
- c++ - C++ 模板;作为模板参数传递的函数的自动推导返回类型;
- groovy - 比较 Jenkins 管道中的日期