Rewrite and FastCGI

Hello everyone!

I’m trying to use rewrite and fastcgis with Nginx 1.2.1 (from the
Debian Wheezy package).

My root directory looks like this:
/www/dir1
/www/dir2

In my server’s file I have this:
root /www/;

    # dir1
    location /dir1 {
            rewrite ^(.*) https://$host$1 permanent;
    }

    # php5-fpm
    location ~ \.(php|phtml)$ {
                    include         /etc/nginx/fastcgi_params;
                    fastcgi_pass 

unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_param SCRIPT_FILENAME
/www/$fastcgi_script_name;
}
}

The rewrite is working great for a html page (for example) but not for
a php page.
Same results with:
root /www/;

    location /dir1 {
            rewrite ^(.*) https://$host$1 permanent;
            # php5-fpm
            location ~ \.(php|phtml)$ {
                    include         /etc/nginx/fastcgi_params;
                    fastcgi_pass 

unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_param SCRIPT_FILENAME
/www/$fastcgi_script_name;
}
}

I need to use php’s fastcgi for dir1 and dir2 but only to redirect
dir1 in https. To be honest I try many settings but without any
results.
It seems that fastcgi (or upstream) have always the priority on
location.

A solution could be to add a rewrite in the php’s location too to have
something like this (but that seems rally ugly to me):
root /www/;

    location /dir1 {
            rewrite ^(.*) https://$host$1 permanent;
            # php5-fpm
            location ~ \.(php|phtml)$ {
                    include         /etc/nginx/fastcgi_params;
                    fastcgi_pass 

unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_param SCRIPT_FILENAME
/www/$fastcgi_script_name;
}
}

    # php5-fpm
    location ~ \.(php|phtml)$ {
                    include         /etc/nginx/fastcgi_params;
                    fastcgi_pass 

unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_param SCRIPT_FILENAME
/www/$fastcgi_script_name;
}
}

Any help would be really appreciated!

Thanks for reading anyway.

Regards.

On Tue, Oct 09, 2012 at 02:03:40PM +0200, Thomas M. wrote:

Hi there,

/www/$fastcgi_script_name;
}
}

The rewrite is working great for a html page (for example) but not for
a php page.

One request is handled in one location.

You have

location /dir1 {}
location ~ \.(php|phtml)$ {}

Possibly what you want is

location ^~ /dir1 {}
location ~ \.(php|phtml)$ {}

or maybe

location /dir1 {}
location / {
  location ~ \.(php|phtml)$ {}
}

See http://nginx.org/r/location for details.

(Possibly what you want is some other configuration: the important thing
to keep in mind is: for this request, which one location do you wish to
handle it? Then configure the locations accordingly.)

f

Francis D. [email protected]

Hi Francis.

FYI I read the documentation before to send my first email but I
misunderstood the part about “^~” and also I didnt clearly realized
that “One request is handled in one location”.

So with your clarification I was able to make something working:

DocumentRoot

root /www/;

dir1

location /dir1 {
rewrite ^(.*) https://$host$1 permanent;
}

dir2

location /dir2 {
# php5-fpm
location ~ .(php|phtml)$ {
fastcgi_param PHP_VALUE
“include_path=/www/dir2:/www/dir2/common/include:.”;
fastcgi_param ENVIRONMENT_NAME devs;
fastcgi_param DB_CONF_DIRECTORY /etc/itf/db/devs/;

  include /etc/nginx/sites-conf.d/php-fpm;
}

}
# All others
location / {
# php5-fpm
location ~ .(php|phtml)$ {
fastcgi_param ENVIRONMENT_NAME devs;
fastcgi_param DB_CONF_DIRECTORY /etc/db/devs/;

  include /etc/nginx/sites-conf.d/php-fpm;
}
}

It seems to work as expected; I guess I could use “^~” too but I
didn’t tried yet.
At first I wanted to avoid repetition of the php-fpm’s part but I
didn’t realized that it wasn’t doable.

Thanks for your help and again this is really appreciated!

Regards.

2012/10/9 Francis D. [email protected]:

I’ve tried various combinations of burst=2, nodelay, 1r/s or 1r/m, with
and
without limit_conn, with and without keepalive, with and without
“location
/”, etc… and requests are never being limited, as shown by the
access.log
entries below:

Posted at Nginx Forum:
http://forum.nginx.org/read.php?2,231534,231568#msg-231568

Hi!

2012/10/10 Francis D. [email protected]:

English is not my native language and I’m really bad at it so this can
be the explanation of my misunderstanding. :confused:
Anyway maybe the section about location could be reorganized a bit to
explain possibilities ([ = | ~ | ~* | ^~ ]) in a dedicated part.

That looks reasonable. “^~” only matters if you have (top level) regex
matches – which here, you don’t.

(Avoiding top level regex matches makes it very easy to know which
location{} will match any particular request. That’s usually considered
a Good Thing.)

Ok, good to know.

In this case, you want different fastcgi_param lines for different php
scripts – so repeating the common config (by using “include”) and adding
the specific parts, like you have done, is probably the best way.
Thanks for this confirmation.

2012/10/11 Thomas M. [email protected]:

Hi!

Hello.

English is not my native language and I’m really bad at it so this can
be the explanation of my misunderstanding. :confused:
Anyway maybe the section about location could be reorganized a bit to
explain possibilities ([ = | ~ | ~* | ^~ ]) in a dedicated part.

English isn’t my native language, too, but regular expressions
(“regex” resp. “RegExp”) is such a wide-spreaded topic, that you could
read about the basics at wikipedia, too ->
http://en.wikipedia.org/wiki/Regular_expression
Choose the translation in your mother tongue at the left hand sidebar
:wink:

Best regards, Andre

Hello Andre.

2012/10/11 Andre J. [email protected]:

(“regex” resp. “RegExp”) is such a wide-spreaded topic, that you could
read about the basics at wikipedia, too ->
http://en.wikipedia.org/wiki/Regular_expression
Choose the translation in your mother tongue at the left hand sidebar :wink:

Best regards, Andre

Indeed, you are right.

Regards.

On Tue, Oct 09, 2012 at 04:50:32PM +0200, Thomas M. wrote:

Hi there,

great that you found a configuration that works.

FYI I read the documentation before to send my first email but I
misunderstood the part about “^~” and also I didn’t clearly realized
that “One request is handled in one location”.

Having re-read the docs, I can see that the interpretation could be
made clearer. Can you suggest any wording that might have helped you
understand then, what you do now? Maybe it can become easier for the
next person :wink:

Would adding a link to
http://nginx.org/en/docs/http/request_processing.html#simple_php_site_configuration
have helped, do you think? It is an example rather than complete
documentation, and it leaves out the “^~” thing, so maybe it wouldn’t
have been directly useful.

So with your clarification I was able to make something working:

location /dir1 {
rewrite ^(.*) https://$host$1 permanent;
}

location /dir2 {
# php5-fpm
location ~ .(php|phtml)$ {
fastcgi_param PHP_VALUE
“include_path=/www/dir2:/www/dir2/common/include:.”;
fastcgi_param ENVIRONMENT_NAME devs;
fastcgi_param DB_CONF_DIRECTORY /etc/itf/db/devs/;

  include /etc/nginx/sites-conf.d/php-fpm;
}

}

It seems to work as expected; I guess I could use “^~” too but I
didn’t tried yet.

That looks reasonable. “^~” only matters if you have (top level) regex
matches – which here, you don’t.

(Avoiding top level regex matches makes it very easy to know which
location{} will match any particular request. That’s usually considered
a Good Thing.)

At first I wanted to avoid repetition of the php-fpm’s part but I
didn’t realized that it wasn’t doable.

In this case, you want different fastcgi_param lines for different php
scripts – so repeating the common config (by using “include”) and
adding
the specific parts, like you have done, is probably the best way.

Thanks for your help and again this is really appreciated!

You’re welcome.

All the best,

f

Francis D. [email protected]