Question

Nginx - Wordpress in a Sub-directory

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


Submit an answer

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!

Sign In or Sign Up to Answer

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.

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.

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

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

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