nginx serving php script instead of executing

David asked:

I have a website that was recently migrated from Cold Fusion to WordPress. To ensure that old links were still valid, our developer wrote a redirect script that translates old URLs into new URLs.

I set up a virtual redirect, but for some reason old style URLs (e.g., ‘www.mysite.com/page.cfm?___BIG_NASTY_STRING’) get sent to the text of the php script or attempt to actually download the file – it doesn’t execute.

Here’s the relevant portion of my config file:

location ~ .php$ {
    try_files $uri =404;
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_param HTTP_HOST $host;
    include fastcgi_params;
    }

location ~* .(js|css|png|jpg|jpeg|gif|ico)$ {
    expires max;
    log_not_found on;
    }

#Redirects for old CFML-style links

location /page.cfm {
    alias /legacy/redirect.php;
}

Does anyone have any idea how to get redirect.php to execute instead of just displaying or downloading?

Thanks!

My answer:


Here is your problem:

location /page.cfm {
    alias /legacy/redirect.php;
}

This is not a redirect. You have just asked nginx to serve exactly that file, which it does faithfully.

In this case you need an internal rewrite which will restart processing the request with the PHP file you want to serve. The rewrite directive is used for this. For instance:

location /page.cfm {
    rewrite . /legacy/redirect.php last;
}

View the full question and answer on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.