Report this

What is the reason for this report?

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

Posted on November 4, 2016

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.



This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Had to add this piece of code to both the /sites-available/default and /sites-enabled/digitalocean:

server { […] if (!-e $request_filename) { rewrite ^.*$ /index.php last; }

[…] }

It’s working for me now.

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

Your Nginx configuration seems mostly correct for supporting pretty permalinks in WordPress. However, there are a few things you can check and modify to ensure it works correctly.

1. Ensure WordPress Configuration

Make sure that your WordPress installation is set to use pretty permalinks:

  • Go to your WordPress admin panel → Settings → Permalinks.
  • Select the permalink structure you want (other than the default plain option) and save the changes.

2. Verify Nginx Configuration

Your Nginx configuration for handling WordPress permalinks seems fine with the try_files $uri $uri/ /index.php?$args; directive. This line is essential for pretty permalinks to work.

3. Nginx Configuration File Permissions

Ensure that your Nginx configuration file permissions are correct:

  • The Nginx user (usually www-data or nginx) should have read access to your configuration files.

4. Restart or Reload Nginx

After making any changes in the Nginx configuration, you must reload or restart the Nginx service:

sudo systemctl reload nginx

5. Check PHP-FPM

Ensure that PHP-FPM is running and the socket path in your Nginx config matches the actual path:

  • Check PHP-FPM status: sudo systemctl status php7.0-fpm (adjust the version number as per your setup).
  • The fastcgi_pass directive in your Nginx configuration should match the PHP-FPM socket path.

6. File Ownership and Permissions

Ensure that the WordPress files are owned by the correct user (usually the Nginx user) and have the correct permissions:

sudo chown -R www-data:www-data /var/www/html
sudo chmod -R 755 /var/www/html

Adjust www-data and /var/www/html as per your setup.

7. Check WordPress and Server Logs

  • WordPress logs (if debugging is enabled in wp-config.php) might contain useful information.
  • Even though you mentioned the Nginx error log is empty, keep an eye on /var/log/nginx/error.log for any errors that might occur when you try to access a page.

8. Check for Conflicting Server Blocks

  • Make sure there are no conflicting server blocks in other config files under /etc/nginx/sites-available/ or /etc/nginx/sites-enabled/.

9. Conflict with Self-Signed Certificate

Since you’re using a self-signed SSL certificate, ensure that there’s no issue with the SSL configuration that might be causing the problem.

Conclusion

If the problem persists after checking these aspects, you might want to consult with a developer or a sysadmin. Sometimes, issues like this can be specific to your server’s setup or other configurations not covered in a standard setup.

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.