php - Laravel:如何允许带有 .php 文件扩展名的路由

标签 php laravel nginx nginx-config

我正在使用 homestead(所以我处理 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 之前没有那个“配置”)

最佳答案

您的配置包含以下内容:

fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;

fastcgi_script_name is ...

request URI or, if a URI ends with a slash, request URI with an index file name configured by the fastcgi_index directive appended to it. This variable can be used to set the SCRIPT_FILENAME and PATH_TRANSLATED parameters that determine the script name in PHP. For example, for the “/info/” request with the following directives



这意味着,当请求 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 的请求。在路径中。

关于php - Laravel:如何允许带有 .php 文件扩展名的路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55272097/

相关文章:

php - Laravel 查询生成器获取自定义属性

javascript - getElementsByClassName 从 php foreach 获取一个元素

php - 调用未定义的方法 Illuminate\Auth\GenericUser::fill()::when update

Laravel gettext 还是本地化?

laravel 访问 Blade 中的模型常量

PHP/MySQL : Insert record if doesnt exist

php - 在 nginx 中为 symfony2 创建一个 block

ubuntu - init.d 脚本不会在重新启动时启动,但可以从命令行运行

nginx - Docker版本1.13.1,Docker Swarm,jwilder/nginx-proxy无法作为Docker服务启动

php - 使用 nginx 时任何 php 文件都会出现 404 错误