首页 > 解决方案 > 允许访问位置块 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;
        }
}

标签: nginx

解决方案


您当前的配置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 配置中的其他块。


推荐阅读