为什么nginx conf中的附加位置返回404代码?
我的虚拟主机有以下配置:为什么nginx conf中的附加位置返回404代码?,nginx,nginx-location,Nginx,Nginx Location,我的虚拟主机有以下配置: server { listen 80; root /var/www/home; access_log /var/www/home/access.log; error_log /var/www/home/error.log; index index.php index.html index.htm; server_name home; location / { try_files $uri $u
server {
listen 80;
root /var/www/home;
access_log /var/www/home/access.log;
error_log /var/www/home/error.log;
index index.php index.html index.htm;
server_name home;
location / {
try_files $uri $uri/ /index.php?$args; #if doesn't exist, send it to index.php
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/run/php/php-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PHP_VALUE "error_log=/var/www/home/php_errors.log";
}
location ~* /Admin {
allow 127.0.0.1;
deny all;
}
}
当我试图访问page/Admin时,nginx返回404代码,其中包含由php生成的成功html内容。删除带有/Admin的位置后,一切都很顺利
如何了解附加位置的问题?您应该阅读以了解各种位置块的优先顺序
因此,您可以将regex位置置于location~\.php$
块上方,以使其优先,或者将其更改为:
location ^~ /Admin { ... }
这是一个优先于任何正则表达式位置的前缀位置(在这种情况下,其在文件中的顺序变得无关)
第二个问题是allow 127.0.0.1
语句的用途。您是否希望从127.0.0.1的客户端执行前缀为/Admin
的.php
文件
管理员位置块不包含要执行.php
文件的代码。如果要执行前缀为/Admin
的.php
文件,可以尝试:
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/run/php/php-fpm.sock;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PHP_VALUE "error_log=/var/www/home/php_errors.log";
}
location ^~ /Admin {
allow 127.0.0.1;
deny all;
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/run/php/php-fpm.sock;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
您可能希望使用include
指令将常用语句移动到单独的文件中
看