简体   繁体   中英

Laravel: How to allow routes with .php file extension

I'm using homestead (so I deal with Nginx) and I want to match some routes that can contain ".php". My nginx config file:

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;
}

I suppose and I hope it has to do with an nginx setting. because I followed this solution https://laracasts.com/discuss/channels/general-discussion/routes-with-php-file-extensions and I almost got it working but not the exact way I want (which is without that "config" before the URL)

Your configuration contains the following:

fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;

And 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

That means, when a request URI contains .php it is treated as if it is a request for a PHP file, and if that PHP file doesn't exist an error is returned by nginx -- it never reaches your application.

The solution is to force fastcgi_script_name to always equal your application's entry point, in this case that's index.php . You can edit it in your location block like this:

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;
}

Your application will now receive every request, including those that have .php in the path.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM