Accessing "non-Wordpress" directory in website root

Hi,

I have found the Wordpress on Nginx tutorials on the rtCamp site very helpful, but was wondering if you could recommend how to access "non-wordpress" subdirectories with Nginx.

I currently have Wordpress installed in /blog where / is the root of the site htdocs. I have created a folder at /api with a php file within and am trying to access the file through www.site.com/api.

The Nginx configuration has the root set to /blog and www.site.com goes to the Wordpress installation as expected. I have set a location /api under the server to have root at /api. When accessing www.site.com/api, a 404 occurs and goes to the Wordpress 404 page. Could you recommend a change in Nginx configuration to get this to work? Thanks.

Domain of site is geometrystash.com Current site conf file:

server {  
    server_name  www.geometrystash.com;  
    rewrite ^(.*) http://geometrystash.com$1 permanent;  
}  

server {  
        listen 80;  
        server_name geometrystash.com;  
        root /var/www/geometrystash.com/htdocs/blog;  
                index index.php;  
        include /etc/nginx/security;  

# Logging --  
access_log  off;  
error_log  /var/log/nginx/geometrystash.com.error.log warn;  

        location = /favicon.ico {  
                log_not_found off;  
                access_log off;  
        }  

        location = /robots.txt {  
                allow all;  
                log_not_found off;  
                access_log off;  
        }  

        # serve static files directly  
        location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico|html|xml|txt)$ {  
            access_log        off;  
            expires           max;  
        }  

        location /api {  
                try_files $uri $uri/ /index.php?$args;  
        root /var/www/geometrystash.com/htdocs;  
            index index.php;  
    }  

        location ~ \.php$ {  
        try_files $uri $uri/ /index.php?$args;  
                fastcgi_pass unix:/var/run/php5-fpm/geometrystash.com.socket;  
                fastcgi_index index.php;  
                include /etc/nginx/fastcgi_params;  
        }  
}

There are many ways to get this working.

I will do this as follows:

First correct your root location to point to htdocs

root /var/www/geometrystash.com/htdocs;

Then add a location block as below:

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

Above will make sure if a some other file/directory exist, it will be served first. If that fails, request will be checked inside /blog/ i.e. WordPress dir.

Now, it looks like you have but WordPress is blog folder but you do not want WordPress URLs to contain /blog/ in them.

For this, follow some instructions from: http://codex.wordpress.org/Giving_WordPress_Its_Own_Directory

I think changing WordPress address (URL) inside Dashboard >> Settings >> General will be enough.

Additionally, you may need to copy index.php from /blog/ to / and change require('./wp-blog-header.php'); => require('./blog/wp-blog-header.php');

Try above. If you face any issues, just let us know.

1 Like

Thanks for the help, using your edits, the custom directory works now , but the wordpress installation’s previous options were interfering with the setup so I tried a new install of WP. I have created a config file manually, and included a modified index.php in /. However, WP goes into a redirect loop. When I strip out the /blog part in the index.php in /, a server error occurs which is expected since the header php is in the /blog directory.
Accessing the setup page from http://site.com/blog/ works correctly, but that was what we were trying to avoid.
Is this a problem on the Nginx config or on the wordpress side? I feel that we’re making a small error here.

It looks like /blog is not redirecting to /blog/

You cam try adding a line like outside location {} blocks.

rewrite /blog /blog/;

The rewrite didn’t seem to solve the problem, but using my modified configuration below (which is the same as the previous):

server {  
    server_name  www.geometrystash.com;  
    rewrite ^(.*) http://geometrystash.com$1 permanent;  
}  

server {  
        listen 80;  
        server_name geometrystash.com;  
        root /var/www/geometrystash.com/htdocs;          
        index index.php;  
		include /etc/nginx/security;  

# Logging --  
access_log  off;  
error_log  /var/log/nginx/geometrystash.com.error.log warn;  

        location = /favicon.ico {  
                log_not_found off;  
                access_log off;  
        }  
   
        location = /robots.txt {  
                allow all;  
                log_not_found off;  
                access_log off;  
        }  

        # serve static files directly  
        location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico|html|xml|txt)$ {  
            access_log        off;  
            expires           max;  
        }  

        location / {  
            try_files $uri $uri/ /blog/$uri /blog/$uri/ /blog/index.php?args;  
        }  
   
        location ~ \.php$ {  
		try_files $uri $uri/ /index.php?$args;  
                fastcgi_pass unix:/var/run/php5-fpm/geometrystash.com.socket;  
                fastcgi_index index.php;  
                include /etc/nginx/fastcgi_params;  
        }  
}

And setting the home wp_option to http://geometrystash.com/ and siteurl wp_option to http://geometrystash.com/blog/.

The Wordpress docs for a pre-existing subdirectory install say to set siteurl to the actual path of the install including the subdirectory. However, it is the opposite and you don’t want that as you can see above.

Now visiting the site such as http://geometrystash.com works fine and the url shows up correctly as the root of the domain. This makes sense due to our try_files option.
But you must set the home wp_option to the actual path of install so that the administrator pages work. Dashboard pages will now have a /blog added into the url.

So now the public side of the site works, but the dashboard site still has /blog in it to function.

This sort of makes sense as the nginx would try /blog/adminpage.php, but if the home option is set to just the domain, the admin pages will not load. Not a big problem, but should be solvable.

If you move WordPress to /blog/ then wp-admin will become /wp-admin/.

Goals of http://codex.wordpress.org/Giving_WordPress_Its_Own_Directory are:

  1. Put your WordPress in /blog/
  2. Do NOT add /blog/ in front-end permalink (at the beginning)
  3. /wp-admin/ will be accessible from /blog/wp-admin/
  4. Uploaded files will have /blog/wp-content/uploads/ path.

Anyway, it looks like your problem is solved! :slight_smile: