Laravel:如何使用.php文件扩展名允许路由

时间:2019-03-21 00:34:27

标签: php laravel nginx nginx-config

我正在使用宅基(所以我要使用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,但我几乎可以正常使用它,但不是我想要的确切方式(在URL之前没有该“ config”)

1 个答案:

答案 0 :(得分:0)

您的配置包含以下内容:

fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;

还有fastcgi_script_name is ...

  

请求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的请求。

相关问题