2

Im working on a Laravel app hosted via Nginx that is replacing an old system that ran on Apache 2.2.

The new system must support all urls currently supported by the old system.

Most urls are like this, and work fine:

https://somesite.com/some/path?someProp=1

However, some urls have a trailing backslash AND a query string, like the below:

https://somesite.com/some/path/?someProp=1

(While such links are surely bad practice, the app must support them for backwards compatibility with other related systems).


In my app, I've found that when the urls have a trailing backslash AND a query string, the query string and path get clobbered somewhere along the way, for example, given this code:

public function boot(): void
{
    $uri   = $_SERVER['REQUEST_URI'];
    $query = $_SERVER['QUERY_STRING'];
}

For urls without a trailing slash I get:

$uri === https://somesite.com/some/path?someProp=1

$query === someProp=1


But for urls with a trailing slash I get:

$uri === https://somesite.com/some/path//1

$query === ''

How can I configure Nginx to set the $request_uri and $query_string values properly when a url may contain a trailing backslash AND a query string?

Nginx server config:

server {
    listen 80;
    listen 443 ssl;

    server_name somesite.com;

    ssl_certificate /etc/ssl/wildcard/tls.crt;
    ssl_certificate_key /etc/ssl/wildcard/tls.key;

    root /home/httpd/htdocs/public;

    location / {
         try_files $uri $uri/ /index.php$is_args$args;
    }

    location ~ \.php$ {
        include includes/php-fpm.conf;

        fastcgi_pass unix:/socket/monolith.php-fpm.sock;

        fastcgi_param  PHP_VALUE   "auto_prepend_file=None
        auto_append_file=None";

    }
}

php-fpm.conf

fastcgi_split_path_info ^(.+\.php)(/.+)$;

fastcgi_param HTTP_PROXY "";

fastcgi_index index.php;

include fastcgi_params;

fastcgi_buffer_size 128k;
fastcgi_buffers 4 256k;
fastcgi_busy_buffers_size 256k;

#fixes timeouts
fastcgi_read_timeout 600;

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
#astcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param SCRIPT_URL $uri;
fastcgi_param SCRIPT_URI $scheme://$http_host$uri;

fastcgi_params:

fastcgi_param  QUERY_STRING       $query_string;
fastcgi_param  REQUEST_METHOD     $request_method;
fastcgi_param  CONTENT_TYPE       $content_type;
fastcgi_param  CONTENT_LENGTH     $content_length;

fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;
fastcgi_param  REQUEST_URI        $request_uri;
fastcgi_param  DOCUMENT_URI       $document_uri;
fastcgi_param  DOCUMENT_ROOT      $document_root;
fastcgi_param  SERVER_PROTOCOL    $server_protocol;
fastcgi_param  REQUEST_SCHEME     $scheme;
fastcgi_param  HTTPS              $https if_not_empty;

fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
fastcgi_param  SERVER_SOFTWARE    nginx/$nginx_version;

fastcgi_param  REMOTE_ADDR        $remote_addr;
fastcgi_param  REMOTE_PORT        $remote_port;
fastcgi_param  SERVER_ADDR        $server_addr;
fastcgi_param  SERVER_PORT        $server_port;
fastcgi_param  SERVER_NAME        $server_name;

# PHP only, required if PHP was built with --enable-force-cgi-redirect
fastcgi_param  REDIRECT_STATUS    200;
Wesley Smith
  • 19,401
  • 22
  • 85
  • 133
  • Maybe you'd add your `server` block from nginx config to your question? – Ivan Shatsky Jun 29 '20 at 18:26
  • @IvanShatsky, indeed, added that and the php-fpm.conf settings – Wesley Smith Jun 29 '20 at 18:30
  • May I ask you to add `fastcgi_params` content too? – Ivan Shatsky Jun 29 '20 at 18:40
  • @IvanShatsky added – Wesley Smith Jun 29 '20 at 19:15
  • Using **exactly** the same config I can't reproduce your issue. I'm using [this](https://gist.github.com/ivanshatsky/7980be97979fb2bdd8ef3b605543827e) script for debugging nginx configurations, you can see the results [here](http://debug.ivan.shatsky.net/some/path?someProp=1) and [here](http://debug.ivan.shatsky.net/some/path/?someProp=1). Can't even guess what can be the source of problem. What would you get in response of these requests if you put this script on your server? – Ivan Shatsky Jun 29 '20 at 22:46

0 Answers0