Nginx Configuration. How to simulate Wordpress subdirectory?

Posted September 15, 2016 16.9k views

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

What I want to achieve up on visit to is that content is served from /srv/www/ as you would expect, but upon visit to the Wordpress site is served from /srv/www/

I’m currently using the following server configuration:

server {

    listen 80;


    root  /srv/www/;
    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
    # 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/;
        try_files $uri $uri/ /index.php?$args;


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

        root /srv/www/;

        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 is working fine (as kind of expected) with no PHP being executed, however 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: <- Static <- Dynamic

Thanks for your time!

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.

Submit an Answer
1 answer

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

root /srv/www/;

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