【问题标题】:Laravel: How to allow routes with .php file extensionLaravel:如何允许带有 .php 文件扩展名的路由
【发布时间】:2019-08-11 19:35:03
【问题描述】:

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

【问题讨论】:

    标签: php laravel nginx nginx-config


    【解决方案1】:

    您的配置包含以下内容:

    fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
    

    还有fastcgi_script_name is...

    请求 URI,或者,如果 URI 以斜杠结尾,则请求带有由 fastcgi_index 指令配置的索引文件名附加到它的 URI。此变量可用于设置确定 PHP 中脚本名称的 SCRIPT_FILENAME 和 PATH_TRANSLATED 参数。例如,对于带有以下指令的“/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 的请求。

    【讨论】:

      猜你喜欢
      • 2012-03-09
      • 1970-01-01
      • 2014-02-09
      • 2017-11-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-20
      • 1970-01-01
      相关资源
      最近更新 更多