Correct WordPress + Nginx + php-fpm Configuration

ddrjm asked:

I have a website running on nginx + php-fpm and I’m looking for a config file that actually shows me a 404 and some other error pages, because the current state of my config file only changes the URL and doesn’t show me a 404 page. For example.

I go to and I’m greeted with the homepage instead of the 404 page.

Also, is the try_files syntax “safe” to use or should I use rewrites?

I’ve read the nginx documentation extensively and the wordpress codex config too.

Below I’l paste my Config file to see if someone can shed some light on the subject.

server {
    root home/;

    access_log logs/;
    error_log logs/ notice;

    charset utf-8;
    index index.php;
    location / {
        #try_files $uri $uri/ /index.php$is_args&$args;
        try_files $uri $uri/ /index.php?$args;

    location ~ \.php$ {
        #try_files $uri =404;
        fastcgi_split_path_info  ^(.+\.php)(.*)$;
        fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
        fastcgi_param PATH_INFO       $fastcgi_path_info;
        include fastcgi_params;

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

    location ~ /\. {
         deny all; 

       error_page   404             /index.php?error=404;

And also fastcgi_params included above

fastcgi_param  QUERY_STRING       $query_string;
fastcgi_param  REQUEST_METHOD     $request_method;
fastcgi_param  CONTENT_TYPE       $content_type;
fastcgi_param  CONTENT_LENGTH     $content_length;

fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;
fastcgi_param  REQUEST_URI        $request_uri;
fastcgi_param  DOCUMENT_URI       $document_uri;
fastcgi_param  DOCUMENT_ROOT      $document_root;
fastcgi_param  SERVER_PROTOCOL    $server_protocol;

fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
fastcgi_param  SERVER_SOFTWARE    nginx/$nginx_version;

fastcgi_param  REMOTE_ADDR        $remote_addr;
fastcgi_param  REMOTE_PORT        $remote_port;
fastcgi_param  SERVER_ADDR        $server_addr;
fastcgi_param  SERVER_PORT        $server_port;
fastcgi_param  SERVER_NAME        $server_name;

# PHP only, required if PHP was built with --enable-force-cgi-redirect
fastcgi_param  REDIRECT_STATUS    200;

Also, does nginx read location from top to bottom? if so, when it hits fastcgi_pass does it read/execute the configuration below it or goes directly to php?

Edit: X-posted from WP StackExchange

My answer:

You should always prefer try_files to rewrites.

The only real issues I see with your configuration are:

        try_files $uri $uri/ /index.php?$args;

For WordPress it’s not necessary to pass in the $args; it gets them from the environment instead. My working configuration has:

        try_files $uri $uri/ /index.php;

Second, you have a stray / here:

        fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;

This might not be a problem, but for sanity’s sake I wouldn’t leave it.

        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

View the full question and any other answers on Server Fault.

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