我从主机的子位置获取php页面上的404错误。我的配置是:
server {
listen 80;
root /var/www/html/nisite;
index index.php index.html index.htm;
server_name www2.company.com wp-newsite-stg-02.company.com;
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_index index.php;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
include fastcgi_params;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
# =================================
location /office {
root /var/www/html/oldsite;
}
}
当我去http://www2.company.com/office
我可以看到页面时,所有的静态资产都被提供,但是当我尝试访问http://www2.company.com/office/php/form-process.php
时,我得到404错误。
为什么location ~ \.php$
没有处理这个请求呢?
谢谢。
location
块从周围区域的$document_root
语句继承root
的值。您正在从两个单独的根运行PHP脚本,因此您需要两个单独的location
块来处理它们。
解决方案是使用嵌套的location
块。
例如:
location ^~ /office {
root /var/www/html/oldsite;
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $request_filename;
}
}
使用^~
修饰符确保正确的location
处理.php
文件。有关详细信息,请参阅this document。使用try_files
语句来避免passing uncontrolled requests to php