Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I am trying to rewrite a specific path using nginx. I have setup my server using nginx/php-fpm/php and it is working fine.

I have the following server configuration:

server {
    listen 80;

    server_name domain.com;
    root /srv/www/domain.com/public;

    location ~ ^/index.php($|/) {
        fastcgi_split_path_info ^(.+.php)(.*)$;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param SCRIPT_NAME $fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
        fastcgi_pass   127.0.0.1:9000;
        include        fastcgi_params;
    }

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

And this works. I.e. when I go to http://domain.com it renders the php file just fine. However I am now trying to rewrite a specific path /update/whatever to /update.php?var=whatever. In order to do this I have added the following location block to my config (before the location ~ ^/index.php($|/) { block):

location ~* ^\/update/(\w+)$ {
    rewrite ^/update/(\w+)$ /update.php?browser=$1 last;
}

But when I try to access the URL http://domain.com/update/whatever it downloads the php file instead of serving the parsed output from PHP.

I have also tried adding the fastcgi directives to that location block with the same result. Anybody can tell me what I have messed up in my config?

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

This line:

location ~ ^/index.php($|/) {

tells nginx to pass only index.php to php interpreter and you need it to also apply to other php files so more generic rule is required here:

location ~ ^/(.*).php($|/) {
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.