Forum: NGINX Rewrite and FastCGI.

Posted by Thomas Martin (Guest)
on 2012-10-09 14:04
(Received via mailing list)
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.
Posted by Francis Daly (Guest)
on 2012-10-09 14:53
(Received via mailing list)
On Tue, Oct 09, 2012 at 02:03:40PM +0200, Thomas Martin 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 Daly        francis@daoine.org
Posted by Thomas Martin (Guest)
on 2012-10-09 16:51
(Received via mailing list)
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 Daly <francis@daoine.org>:
Posted by tysonlee (Guest)
on 2012-10-10 11:33
(Received via mailing list)
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
Posted by Francis Daly (Guest)
on 2012-10-10 14:37
(Received via mailing list)
On Tue, Oct 09, 2012 at 04:50:32PM +0200, Thomas Martin 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 ;-)

Would adding a link to
http://nginx.org/en/docs/http/request_processing.h...
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 Daly        francis@daoine.org
Posted by Thomas Martin (Guest)
on 2012-10-11 09:48
(Received via mailing list)
Hi!

2012/10/10 Francis Daly <francis@daoine.org>:

>
English is not my native language and I'm really bad at it so this can
be the explanation of my misunderstanding. :/
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.
Posted by Andre Jaenisch (Guest)
on 2012-10-11 13:13
(Received via mailing list)
2012/10/11 Thomas Martin <tmartincpp@gmail.com>:
> Hi!

Hello.
> English is not my native language and I'm really bad at it so this can
> be the explanation of my misunderstanding. :/
> 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 
;-)

Best regards, Andre
Posted by Thomas Martin (Guest)
on 2012-10-11 13:20
(Received via mailing list)
Hello Andre.

2012/10/11 Andre Jaenisch <andrejaenisch@googlemail.com>:
> ("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 ;-)
>
> Best regards, Andre
>

Indeed, you are right.

Regards.
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.