[英]Laravel: How to allow routes with .php file extension
我正在使用宅基地(所以我要使用Nginx),我想匹配一些包含“ .php”的路由。 我的nginx配置文件:
server {
listen 80;
listen 443 ssl http2;
server_name .homestead.test;
root "/home/vagrant/code/test/public";
index index.html index.htm index.php;
charset utf-8;
location / {
try_files $uri $uri/ /index.php?$query_string;
add_header 0 false;
}
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
access_log off;
error_log /var/log/nginx/homestead.test-error.log error;
sendfile off;
client_max_body_size 100m;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.3-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_intercept_errors off;
fastcgi_buffer_size 16k;
fastcgi_buffers 4 16k;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
}
location ~ /\.ht {
deny all;
}
ssl_certificate /etc/nginx/ssl/homestead.test.crt;
ssl_certificate_key /etc/nginx/ssl/homestead.test.key;
}
我想,我希望它与nginx设置有关。 因为我遵循了这个解决方案https://laracasts.com/discuss/channels/general-discussion/routes-with-php-file-extensions ,我几乎可以使用它了,但不是我想要的确切方式(没有该“ “(网址前)
您的配置包含以下内容:
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
而fastcgi_script_name
是 ...
请求URI,或者如果URI以斜杠结尾,则请求URI带有由附加在其上的fastcgi_index指令配置的索引文件名。 此变量可用于设置SCRIPT_FILENAME和PATH_TRANSLATED参数,这些参数确定PHP中的脚本名称。 例如,对于带有以下指令的“ / info /”请求
这意味着,当请求URI包含.php
它将被视为对PHP文件的请求,并且如果该PHP文件不存在,nginx将返回错误-它永远不会到达您的应用程序。
解决方案是强制fastcgi_script_name
始终等于应用程序的入口点,在本例中为index.php
。 您可以像下面这样在位置块中对其进行编辑:
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.3-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root/index.php;
}
您的应用程序现在将收到每个请求,包括路径中带有.php
请求。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.