Nginx - Wordpress in a Sub-directory

June 26, 2015 41.8k views
Nginx Ubuntu

I am running wordpress in a sub-directory /wordpress, I want it to be served at root. I cannot change the application root to /wordpress, because i have a core php application at root, which runs as example.com/some_file.php . My configuration file run wordpress homepage and other pages fine, but wp-admin pages are broken, also any images put in wp-content folder are broken when called from within the theme. My configuration file for rewrites is:


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

location /wordpress {
    index index.php;
    try_files $uri $uri/ /wordpress/index.php?$args;
}
location ~ \.php$ {
  try_files $uri =404;
  include fastcgi_params;
  fastcgi_pass php;
}

code

The link to my site is this and link to wp-admin is here

5 Answers

You can do the below for WP in a sub-folder:

        location /wordpress {
                rewrite ^(/[^/]+)?(/wp-.*) /wordpress/$2 break;
                rewrite ^/wordpress/(.*)$ /wordpress/index.php?q=$1 last;
        }

With this Nginx configuration, I was able to achieve this on Ubuntu 14.04.

server {
        listen 80 default_server;
        listen [::]:80 default_server ipv6only=on;
        root /var/www/html;
        # server_name your_domain.com;

        location / {
                try_files $uri /wordpress/index.php?q=$uri&$args;
        }

        location /wordpress {
                index index.php index.html index.htm;
                try_files $uri $uri/ /index.php?q=$uri&$args;
        }

        error_page 404 /404.html;

        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
                root /usr/share/nginx/html;
        }

        location ~ \.php$ {
                try_files $uri =404;
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
                fastcgi_pass unix:/var/run/php5-fpm.sock;
                fastcgi_index index.php;
                include fastcgi_params;
        }
}

This worked for me on Ubuntu 16.04 and PHP 7.1
The path of my wordpress is in /var/www/html/wordpress/blog

location /blog {
        root /var/www/html/wordpress/;

        access_log /var/log/nginx/blog.access.log;
        error_log /var/log/nginx/blog.error.log;

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

}

The following serves the html root folder (/var/www/html) to the domain subdirectory (example.com/wordpress).

Tested on Ubuntu 18.04, PHP 7.3 & latest nginx.

location ^~ /wordpress {
        # Sets /var/www/html as root of example.com/wordpress
        alias /var/www/html;
        # Get script /var/www/html/index.php
        # It's important to start with a slash "/", 
        # that way it looks for index.php at the root directory, 
        # not in example.com/wordpress/some-slug/index.php
        index /index.php;

        # Add a trailing slash if missing
        if (!-f $request_filename) {
            rewrite [^/]$ $uri/ permanent;
        }

        # First attempt to serve request as file, then
        # as directory, then fall back to index.php
        try_files $uri $uri/ /index.php?$args;
}

# For security reasons, set php settings at root level. 
# This prevents root level php files from showing as plain text.
location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;

        fastcgi_index index.php;
        # Change this to your fpm socket
        fastcgi_pass php:9000;

        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
        include fastcgi_params;
}

Ignore the top answer, it doesn’t work with /wp-login.php.

Here’s a better approach


location / {
        rewrite ^ /wordpress/ permanent;
}

location ^~ /wordpress {
        # Sets /var/www/html as root of example.com/wordpress
        alias /var/www/html;

        # Add a trailing slash if missing
        if (!-f $request_filename) {
            rewrite [^/]$ $uri/ permanent;
        }

        # First attempt to serve request as file, then
        # as directory, then fall back to index.php
        try_files $uri $uri/ /index.php?$args;

        # For security reasons, set php settings at root level. 
        # This prevents root level php files from showing as plain text.
        location ~ \.php$ {
                try_files $uri =404;
                fastcgi_split_path_info ^/wordpress(/.+\.php)(.*)$;

                fastcgi_index index.php;
                # Change this to your fpm socket
                fastcgi_pass php:9000;

                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                fastcgi_param PATH_INFO $fastcgi_path_info;
                include fastcgi_params;
        }
}

Have another answer? Share your knowledge.

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