How can WordPress be made to live at /blog, while another application handles the root directory /, using nginx?

I have the ordinary subdomain setup right now, which works fine.

I've read https://codex.wordpress.org/Nginx and changed my config according to serving WordPress as a subdirectory. In wp-config.php I have

define('WP_HOME','http://example.com/blog');
define('WP_SITEURL','http://example.com/blog');

but I keep getting 404 errors, or WordPress not loading CSS files. I've also tried

location /blog {
  try_files /var/www/wordpress/$uri /var/www/wordpress/$uri/ /blog/index.php?$args;
}

404s again.

This must be an FAQ, but I haven't nailed the search terms for it (/blog being so common doesn't help).

Where can I find a full nginx config file and wp-config settings that have WordPress live at /blog? Do I need to alter any WP settings in its MySQL database?

Here's what mostly works:

  1. Have an nginx config file for the subdomain setup, and ensure that http://wordpress.example.com works.

  2. In the nginx default config, have a location block for /blog that proxies to the subdomain:

    location /blog {
        rewrite ^/blog(/?.*)$ $1 break;  # remove "/blog"
        proxy_set_header Host wordpress.example.com;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_pass http://wordpress.example.com;
    }
    
  3. In wp-config.php, have:

    define('WP_HOME','http://idorecall.com/blig');
    define('WP_SITEURL','http://idorecall.com/blig');
    

I'm saying this "mostly" works because:

  • Some page elements may not be loading. For example, the icomoon icons used by the Avada theme in the footer don't show up.
  • If you access your /blog URL via https, e.g. at http://example.com/blog, but the wordpress subdomain config uses http, browsers will refuse to load many resources, with a "Mixed Content" warning. One solution is to enable HTTPS on the subdomain too (StartSSL has a certificate with one free subdomain). Another, I suspect, is that you'll have to run a Wordpress database search and replace of http://wordpress.example.com with http://example.com/blog.
  • There's an extra request involved with proxy_passing.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.