Rewrite and FastCGI.

Thomas Martin tmartincpp at gmail.com
Tue Oct 9 12:03:40 UTC 2012


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.



More information about the nginx mailing list