C
C
Cyapa2017-06-02 14:17:01
PHP
Cyapa, 2017-06-02 14:17:01

Nginx: How to make rewrite and fastcgi friends?

Hello friends.
Tell me how to make rewrite and fastcgi friends? A bunch of nginx and php-fpm is running on the server.
The configuration is the following:

main
server
{
        listen           80 default_server;
        server_name      example.com;


        root             /home/sites/example.com/;


        access_log       /var/log/nginx/example.com.bots.log advanced flush=1m buffer=32k if=$isBot;
        access_log       /var/log/nginx/example.com.access.log advanced flush=5m buffer=32k if=$isHuman;
        error_log        /var/log/nginx/example.com.errors.log warn;


        location @php
        {
                include      bootstrap/modules/php.conf;
        }

        location /
        {
                try_files    $uri $uri/ /index.php?$query_string;
        }

        location /application
        {
                deny         all;
        }

        location /errors/
        {
                alias        /home/sites/example.com/application/views/errors/;
                allow        all;
        }


        error_page       403 /errors/403.html;
        error_page       404 /errors/404.html;
        error_page       500 /errors/500.html;


        include          bootstrap/modules/gzip.conf;
        include          bootstrap/locations/system.conf;
        include          bootstrap/locations/static.conf;
        include          bootstrap/locations/errors.conf;
        include          bootstrap/locations/php.conf;
}

bootstrap/modules/php.conf
include                    fastcgi_params;

fastcgi_pass               php;
fastcgi_index              index.php;
fastcgi_next_upstream      error timeout;
fastcgi_keep_conn          on;
fastcgi_split_path_info    ^(.+?\.php)(/.*)$;


set $final_path_info       $fastcgi_path_info;
set $final_script_name     $fastcgi_script_name;

try_files                  $fastcgi_script_name = 404;

#fastcgi_param              PATH_INFO          $final_path_info;
fastcgi_param              SCRIPT_FILENAME    $document_root$final_script_name;
fastcgi_param              SCRIPT_NAME        $final_script_name;
fastcgi_param              REQUEST_URI        $request_uri;

bootstrap/locations/php.conf
location ~ ^.+\.php
{
        try_files    false  @php;
}

The goal is to redirect requests to /stubs/api.php to /api/old.
I add a rewrite rule to the server section (immediately after the error_log): But $fastcgi_path_info and $fastcgi_script_name still get the old data. I add a crutch:
rewrite ^/stubs/api\.php(.*)$ /api/old$1 last;
bootstrap/modules/php.conf
include                    fastcgi_params;

fastcgi_pass               php;
fastcgi_index              index.php;
fastcgi_next_upstream      error timeout;
fastcgi_keep_conn          on;

set $final_path_info       "";
set $final_script_name     $uri;

if ($uri ~ "^(.+?\.php)(/.*)$")
{
        set $final_path_info       $2;
        set $final_script_name     $1;
}

try_files                  $fastcgi_script_name = 404;

#fastcgi_param              PATH_INFO          $final_path_info;
fastcgi_param              SCRIPT_FILENAME    $document_root$final_script_name;
fastcgi_param              SCRIPT_NAME        $final_script_name;
fastcgi_param              REQUEST_URI        $request_uri;

It starts working, but /stubs/api.php gets into REQUEST_URI, and /api/old should be there. If you use $uri instead of $request_uri, then index.php is there.
Is it possible to somehow achieve the desired without making changes specific to this case in bootstrap/modules/php.conf (meaning, define REQUEST_URI dynamically, and not insert a hardcoded constant there).
At the moment I'm just doing an additional rewrite of REQUEST_URI on the php side. But I would like to solve the problem more gracefully.
I would also like to find out why rewrite does not affect $fastcgi_path_info and $fastcgi_script_name.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrey, 2017-06-03
@VELIK505

if ($request_uri ~ ^/stubs/api.php) {  
    rewrite ^.*$ /api/old permanent;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question