简体   繁体   中英

PHP keep and pass $_GET in a nginx rewrite

I'm using this rewrite:

location = /example-page {
    try_files $uri /pages/example-page.php;
}

This rewrite is causing $_GET not to be available for some reason.

When I go to example-page.php?test and use print_r($_GET); it returns an empty Array. If however I access the file directly, as in /pages/example-page.php?test

I get Array ( [test] => )

My question is, how can I make it so that all these vars, like $_GET are kept, using the rewrite?

The corrected rewrite as mentioned in comment, while will work, is the least preferred:

location = /example-page {
    try_files $uri /pages/example-page.php$is_args$args;
}

It is sub-optimal, because it checks existence of file <webroot>/example-page for every request to /example-page URI. Sure enough, this file does not exist to begin with. So all the file existence checks are in vain. That, depending on traffic and disk used, will be a reason for performance issues.

Getting rid of try_files to reduce stat system calls is preferable in this case. Simply tell NGINX to route request via PHP-FPM, as well as which filename the PHP-FPM has to use for processing:

location = /example-page {
    fastcgi_param SCRIPT_FILENAME $document_root/pages/example-page.php;
    include fastcgi_params;
    fastcgi_pass unix:/var/run/php-fpm/example.com.sock;
}

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