Nginx dynamic PHP 404 (with URL rewriting)

http-status-code-404nginxPHP

I'm trying to set-up dynamic 404 pages so that I can keep the URL of the missing content, show it on the 404 page and keep my site's template consistent.

Here is my site conf:

server {
    listen       80;
    server_name  www.example.com;

    client_max_body_size 1024M;
    client_body_buffer_size 512M;

    if ($http_host !~* "^www\.example\.com"){
       set $rule_0 1$rule_0;
    }

    if ($rule_0 = "1"){
        rewrite ^/(.*)$ http://www.example.com/$1 permanent;
        break;
    }

    rewrite ^/([a-zA-Z0-9-_]+)$ /profile.php?url=$1 last;

    location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
        expires 1y;
        log_not_found off;
    }

    location / {
        root   /var/www/html/example.com;
        index  index.php index.html index.htm;
    }

    location ~ \.php$ {
        root           /var/www/html/example.com;
        fastcgi_pass  example_fast_cgi;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  /var/www/html/example.com$fastcgi_script_name;
        include        fastcgi_params;
    }

    error_page 404 = @handler;

    location @handler {
        root           /var/www/html/example.com;
        try_files $uri /404.php = 404;   
    }

    location ~ /\.ht {
        deny  all;
    }
}

This seems like it's almost there but any pages that should be seeing the 404 page get a prompt to download the raw PHP file. Adding in the FastCGI parameters didn't do anything.

Best Answer

That should do the trick.

server {
    listen 80;
    server_name example.com;
    return 301 $scheme://www.$server_name$request_uri;
}

server {
    listen 80;
    server_name www.example.com;
    root /var/www/html/example.com;
    index index.php index.html index.htm;
    error_page 404 /404.php;
    location / {
        rewrite ^/([a-zA-Z0-9-_]+)$ /profile.php?url=$1 last;
        location ~ /\. {
            return 404;
        }
        location ~* ^.+\.php$ {
            include fastcgi_params;
            fastcgi_pass php;
         }
    }
}