404 when using pretty permalinks on new WordPress site on LEMP/nginx

November 4, 2016 111 views
Nginx LEMP WordPress Ubuntu 16.04

I've spent two days trying to problem-solve this, but I'm not a developer/coder and can't figure out what I'm doing wrong. I want to use pretty permalinks, but will get 404 errors for each page except home.

I used these DigitalOcean guides for Ubuntu 16.04 for my installation. Disabling my theme and all plugins doesn't solve the problem, nor does using the Nginx Helper plugin.

This is what my config file at /etc/nginx/sites-available/default looks like (comments removed), but I had the same issue with the stock file and just the "try_files $uri $uri/ /index.php?$args;" changed:

server {
        listen 80 default_server;
        listen [::]:80 default_server;
        listen 443 ssl http2 default_server;
        listen [::]:443 ssl http2 default_server;

        server_name mysite.com www.mysite.com;
        include snippets/self-signed.conf;
        include snippets/ssl-params.conf;

        root /var/www/html;
        index index.php index.html index.htm index.nginx-debian.html;

        location = /favicon.ico {
                log_not_found off;
                access_log off;
        }

        location = /robots.txt {
                log_not_found off;
                access_log off;
                allow all;
        }

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

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

        location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php7.0-fpm.sock;
        }

        location ^~ /\.well-known {
                allow all;
        }

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

My /var/log/nginx/error.log file is curiously empty.

Any help is appreciated - thanks in advance.

1 Answer

I would recommend starting here. Since WordPress was originally developed to run on Apache with mod_rewrite some custom configuration is required in order to use it with nginx. There are example configuration files there you can compare your current configuration with. Also, be sure to restart nginx when you make configuration changes so they take effect service nginx restart

Have another answer? Share your knowledge.