Nginx try_files提示下载PHP



我正在AWS Linux 2上设置一个Nginx+PHP web服务器。这是一个全新的安装,安装了nginx和PHP7.4。以下是nginx中的虚拟主机配置文件。

我需要将所有流量重定向到index.php,因为它是一个单页应用程序。

当我访问www.xxx.com/index.php时,php页面呈现良好(因此php肯定在运行(。

当我转到www.xxx.com/login/时,浏览器会提示下载index.php文件,而不是执行它。

有人能帮忙吗?(我已尝试清除浏览器缓存(。

/etc/nginx/conf.d/myapp.conf

server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/vhosts/app.userback.io/frontend/;
index index.php index.html index.htm;
server_name www.myapp.com;
location / {
try_files $uri $uri/ /index.php =404;
}
location ~ .php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+.(?:php|phar))(/.*)$;
fastcgi_intercept_errors on;
fastcgi_index  index.php;
include        fastcgi_params;
fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
fastcgi_param  PATH_INFO $fastcgi_path_info;
fastcgi_pass   php-fpm;
}
}

你能试试吗:

location ~ .php$ {
try_files $uri /404.html;
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;// change it if not valid
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
} 

我在这里找到了答案:NGINX try_files不会传递给PHP。

事实证明,这是位置加上删除404的顺序,使重定向成为内部重定向。

最新更新