Nginx Configuration. How to simulate Wordpress subdirectory?

September 15, 2016 633 views
Nginx PHP WordPress Ubuntu

Basically, I have two separate Git repositories,example.com and wordpress-site.com. Essentially, example.com is purely a static site and wordpress-site.com is unsurprisingly a Wordpress site.

What I want to achieve up on visit to http://example.com is that content is served from /srv/www/example.com/html as you would expect, but upon visit to http://example.com/wordpress the Wordpress site is served from /srv/www/wordpress.com/current/web.

I'm currently using the following server configuration:


server {

    listen 80;

    server_name   example.com;

    root  /srv/www/example.com/html;
    index index.php index.htm index.html;

    # Prevent PHP scripts from being executed inside the uploads folder.
    location ~* /app/uploads/.*\.php$ {

        deny all;

    }

    # I understand If is Evil but readup at https://discourse.roots.io/t/working-subdirectory-multisite-nginx-rules/1503/2
    # and seems the general course of action for file rewrites with 'subdirectories'
    if (!-e $request_filename) {

        rewrite /wp-admin$ $scheme://$host$uri/ permanent;

        rewrite ^(/wordpress/[^/]+)?(/wp-.*) /wp$2 last;
        rewrite ^(/wordpress/[^/]+)?(/plugins.*) /app$2 last;
        rewrite ^(/wordpress/[^/]+)?(/uploads.*) /app$2 last;
        rewrite ^(/wordpress/[^/]+)?(/themes.*) /app$2 last;
        rewrite ^(/wordpress/[^/]+)?(/.*\.php) /wp$2 last;

    }

    # Ok, when we hit /wordpress I'm setting an alias to the new root. As far as I'm aware (could be wrong) this is my only option as I don't have a 'wordpress' directory.
    location /wordpress {

        alias  /srv/www/wordpress-site.com/current/web;
        try_files $uri $uri/ /index.php?$args;

    }

    # Location block to call PHP should only execute when visiting /wordpress
    location ~ /wordpress/.+\.php$ {

        root /srv/www/wordpress-site.com/current/web;

        try_files $uri =404;
        error_page 404 /index.php;

        set $skip_cache 0;

        if ($query_string != "") {
            set $skip_cache 1;
        }

        # Don't cache uris containing the following segments
        if ($request_uri ~* "/wp-admin/|/xmlrpc.php|wp-.*.php|/feed/|index.php|sitemap(_index)?.xml") {
            set $skip_cache 1;
        }

        # Don't use the cache if cookies includes the following
        if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_no_cache|wordpress_logged_in") {
            set $skip_cache 1;
        }

        fastcgi_cache wordpress;
        fastcgi_cache_valid 30s;
        fastcgi_cache_bypass $skip_cache;
        fastcgi_no_cache $skip_cache;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        fastcgi_param DOCUMENT_ROOT $realpath_root;
        fastcgi_pass unix:/var/run/php-fpm-wordpress.sock;

    }

}

The current behaviour with this setup, is that that the static content from http://example.com is working fine (as kind of expected) with no PHP being executed, however http://example.com/wordpress is returning a 404.

I'm almost certain I'm lacking a key area of understanding of Nginx configs is lacking so any helpful incites are greatly appreciated.

In a nutshell:

http://example.com <- Static
http://example.com/wordpress <- Dynamic

Thanks for your time!

1 Answer

You may be better off using an alias instead of root command. The line:

root /srv/www/wordpress-site.com/current/web;

In the location /wordpress block means that nginx will be looking for your wordpress files in /srv/www/wordpress-site.com/current/web/wordpress. This current configuration should work if you rename /srv/www/wordpress-site.com/current/web to /srv/www/wordpress-site.com/current/wordpress and change the root there to /srv/www/wordpress-site.com/current

Have another answer? Share your knowledge.