2

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?

1
  • use another location block, like location ~ /page/ Commented Sep 19, 2015 at 12:21

1 Answer 1

0

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.

Sign up to request clarification or add additional context in comments.

7 Comments

Thank you for the answer. When I put fastcgi_pass into any location I see an error: nginx: [emerg] "fastcgi_pass" directive is duplicate in /etc/nginx/conf.d/fastcgi_params.conf: nginx: configuration file /etc/nginx/nginx.conf test failed
@Ockonal do you have other locations along with those two you posted in the question?
@Ockonal take a look at the Solution 3
thank you, it works in case of: http://example.com/page/foo/bar but fails when: http://example.com/page/foo/bar.html (404)
@Ockonal it looks like there is another location before that I posted, which intercepts .html request. If it is so, you may try put ^/page/ location before any other location
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.