Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do I need to redirect $args in nginx to index.php?

Many PHP frameworks suggest to add this to nginx :

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

to execute index.php on all HTTP requests.

Why do I need $is_args$args? I think the $args are already in the HTTP GET request. So why does nginx need to pass them that way to index.php?

like image 608
Gradient Avatar asked Oct 15 '25 16:10

Gradient


1 Answers

It depends on who wrote index.php. A large number of parameters are sent to PHP from nginx, and usually QUERY_STRING and REQUEST_URI are among them.

If the programmer accesses $_SERVER["QUERY_STRING"] they will receive whatever was appended to the end of /index.php in the try_files statement.

If the programmer accesses $_SERVER["REQUEST_URI"] they will receive the original URI together with the original query string, and anything appended to the end of /index.php in the try_files statement will not affect that.

The two applications that I host (WordPress and MediaWiki) obviously use the latter, because I do not append $is_args$args to the /index.php and it all works fine.

But another application may behave differently.

like image 183
Richard Smith Avatar answered Oct 18 '25 07:10

Richard Smith



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!