Report this

What is the reason for this report?

Nginx - Wordpress in a Sub-directory

Posted on June 26, 2015

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



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!

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.

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

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

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.