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;
        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?


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($|/) {

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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