简体   繁体   中英

Nginx rewrite path to query string

I use nginx (php-fpm) and fatfree framework. I need some redirect logic in my routing engine. It looks like:

http://example.net/page/...

Instead of dots there could be something like:

another.net/someurl
http://another.net/?local_query
another.net/path/to/article.html

It breaks nginx logic and I see 404 error.

My nginx config looks simply:

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

location ~ \.php$ {
    include             /etc/nginx/conf.d/fastcgi_params.conf;

    fastcgi_param       SCRIPT_FILENAME     /var/www/$main_host/www$fastcgi_script_name;
    fastcgi_param       DOCUMENT_ROOT       /var/www/$main_host/www;

    fastcgi_param       PHP_ADMIN_VALUE     upload_tmp_dir=/var/www/$main_host/tmp/upload;
    fastcgi_param       PHP_ADMIN_VALUE     session.save_path=/var/www/$main_host/tmp/sessions;
}

So when I use:

http://example.net/page/another.net/path/to/article.html

Nginx tries to find file in filesystem with name article.html as I understand. How to write rule to ignore any symbols when there is keyword page after domain?

Solution #1:

Probably it doesn't work because there is no fastcgi_pass directive inside /etc/nginx/conf.d/fastcgi_params.conf .

If so, all you need is simply add it to your \\.php$ location. In general case it looks like:

fastcgi_pass 127.0.0.1:9000;

but this line might be different, depending on how PHP-FPM is configured.

Solution #2:

Your current configuration makes Nginx pass request to PHP-FPM only if URI ends with .php .

To make Nginx do it when URI starts with /page/ as well, you should write a new location with almost the same content (except for SCRIPT_FILENAME line) and (important!) put it before existing \\.php$ location.

Copying location content would cause a code duplication, and I would recommend write the config in a slightly different way:

fastcgi_param       SCRIPT_FILENAME     /var/www/$main_host/www$my_script_name;
fastcgi_param       DOCUMENT_ROOT       /var/www/$main_host/www;

fastcgi_param       PHP_ADMIN_VALUE     upload_tmp_dir=/var/www/$main_host/tmp/upload;
fastcgi_param       PHP_ADMIN_VALUE     session.save_path=/var/www/$main_host/tmp/sessions;
include             /etc/nginx/conf.d/fastcgi_params.conf;

location ~ /page/ {
    fastcgi_pass 127.0.0.1:9000;
    set $my_script_name /path/to/file.php;
}

location ~ \.php$ {
    fastcgi_pass 127.0.0.1:9000;
    set $my_script_name $fastcgi_script_name;
}

In this case you will have 2 scenarios:

  • if URI starts with /page/ , Nginx will run /var/www/$main_host/www/path/to/file.php .
  • if URI ends with .php , Nginx will run requested php file, as usual.

Solution #3:

Leave your \\.php$ location as is and put the following location before it:

location ~ ^/page/ {
    include             /etc/nginx/conf.d/fastcgi_params.conf;

    fastcgi_param       SCRIPT_FILENAME     /var/www/$main_host/www/path/to/file.php;
    fastcgi_param       DOCUMENT_ROOT       /var/www/$main_host/www;

    fastcgi_param       PHP_ADMIN_VALUE     upload_tmp_dir=/var/www/$main_host/tmp/upload;
    fastcgi_param       PHP_ADMIN_VALUE     session.save_path=/var/www/$main_host/tmp/sessions;
}

Just replace /path/to/file.php with real path to file which should handle such requests.

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