Question

WordPress post URLs return un-styled Index of root in Nginx

Overview

I deployed a fresh droplet today and configured it with LEMP and installed WordPress to host both my portfolio and a WordPress project. The directories are set up as such:

Root /var/www/domain/

WordPress var/www/domain/wordpress/

I followed DigitalOcean’s tutorials to set up the Ubuntu 22.04 server, install LEMP, configure SSL, and install WordPress.

At this point, everything is working except two crucial items:

  • WordPress pages and posts return an un-styled Index of the root site
  • 404 redirects are not working, but instead, browser error, The site can't be reached, is returned.

Nginx Config

I’ve determined the issue lies within my nginx configuration file, but I’m new to nginx, so I’m not sure where to begin. I chose nginx over apache (where before I have hosted these website with apache) because I will be hosting a laravel project, too.

At the moment, my server is configured as such:

server {
    server_name domain www.domain;
    root /var/www/domain;

    index index.php;

    location / {
        try_files $uri $uri/ =404;
    }

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

    location ~ /\.ht {
        deny all;
    }

    location /wordpress {
        try_files $uri $uri/ /index.php$is_args$args;
    }

    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;
    }

    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/domain/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/domain/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

    error_page 404 /404.php;
    location = /404.php {
        root /var/www/domain;
        internal;
    }
    error_page 500 502 503 504 /50x.php;
    location = /50x.php {
        root /var/www/domain;
        internal;
    }
}

server {
    if ($host = www.domain) {
        return 301 https://$host$request_uri;
    } # managed by Certbot

    if ($host = domain) {
        return 301 https://$host$request_uri;
    } # managed by Certbot

    listen 80;
    server_name domain www.domain;
    return 404; # managed by Certbot
}

The WordPress configuration is outlined in step 3 of the tutorial.

Additionally, I attempted to add error page handling by following the DigitalOcean tutorial, which can be seen in the code above, but as mentioned, 404 pages are returned as The site can't be reached.

.htaccess vs Nginx config

In researching this issue, I’ve learned that .htaccess files are principally for configuring Apache servers. At the moment, I have a rules to redirect to HTTPS, remove .php extensions, and point to a 404 page.

RewriteEngine On

# Redirect
RewriteCond %{HTTPS} off
RewriteCond %{HTTP:X-Forwarded-Proto} =http
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

# Remove extensions
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [NC,L]

ErrorDocument 404 /404.php

Reviewing the Nginx config, I believe certbot is taking care of the redirect, but I’m not sure how to translate the other rules for the Nginx config.

I’ve also read in many answers and suggestions for similar issues to review permalink setting in the WordPress dashboard. I have mine set to domain/wordpress/sample-post/. I’m not sure if there should be a corresponding setting configured in the Nginx config.

For reference, A Zaker’s posted question is similar to mine, but the offered solution unfortunately did not solve my issue.

I appreciate any help.

Show comments

Submit an answer


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!

Sign In or Sign Up to Answer

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.

alexdo
Site Moderator
Site Moderator badge
June 10, 2023

Heya,

The un-styled WordPress pages issue is most likely caused by an incorrect configuration for your static assets location. Let’s update the following section:

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

Replace it with:

  1. location ~* \.(css|gif|ico|jpeg|jpg|js|png)$ {
  2. expires max;
  3. log_not_found off;
  4. try_files $uri $uri/ /wordpress/index.php$is_args$args;
  5. }

This will help Nginx to look for assets in the correct path.

Next, let’s fix the 404 issues. First, update your /wordpress location block as follows:

  1. location /wordpress {
  2. try_files $uri $uri/ /wordpress/index.php$is_args$args;
  3. }

Now, update the error_page 404 directive:

  1. error_page 404 /wordpress/404.php;

And update the corresponding 404 location block:

  1. location = /wordpress/404.php {
  2. root /var/www/domain;
  3. internal;
  4. }

Lastly, let’s address the .htaccess rules. For the “Remove extensions” rule, Nginx should handle PHP files correctly with the configuration you posted, so no need to worry about that. As for handling HTTPS redirects, Certbot is indeed taking care of that in your current configuration.

Don’t forget to restart Nginx after making these changes:

  1. sudo systemctl restart nginx

For more information on configuring Nginx with WordPress, refer to this DigitalOcean tutorial.

Hope that this helps!

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel