Title: Directory access using Nginx
Last modified: August 21, 2016

---

# Directory access using Nginx

 *  [ansonl](https://wordpress.org/support/users/ansonl/)
 * (@ansonl)
 * [13 years ago](https://wordpress.org/support/topic/directory-access-using-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 **[http://www.site.com/api](http://www.site.com/api)**.
 * The Nginx configuration has the root set to **/blog** and [http://www.site.com](http://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 **[http://www.site.com/api](http://www.site.com/api)**,
   a 404 occurs and goes to the WordPress 404 page.
 * I am using Nginx with a socket to PHP-FPM.
    Could you recommend a change in Nginx
   configuration to get this to work? Thanks.

Viewing 6 replies - 1 through 6 (of 6 total)

 *  [Pothi Kalimuthu](https://wordpress.org/support/users/pothi/)
 * (@pothi)
 * [13 years ago](https://wordpress.org/support/topic/directory-access-using-nginx/#post-3757760)
 * Please post your Nginx conf, to see what could be fixed in it.
 *  Thread Starter [ansonl](https://wordpress.org/support/users/ansonl/)
 * (@ansonl)
 * [13 years ago](https://wordpress.org/support/topic/directory-access-using-nginx/#post-3757853)
 * Site domain is geometrystash.com. Nginx server conf file **for this site** posted
   below.
 *     ```
       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;
               }
       }
       ```
   
 *  [Pothi Kalimuthu](https://wordpress.org/support/users/pothi/)
 * (@pothi)
 * [13 years ago](https://wordpress.org/support/topic/directory-access-using-nginx/#post-3757949)
 * Please check out [the modified code](http://pastebin.com/aBht99F2).
 * Basically, there is a missing `location ~ \.php` block inside `location /api`
   block. Nginx would have worked correctly, if you don’t use any PHP files inside
   it and use only static files with `index index.html`. So, when you do use PHP
   files inside /api directory, it goes back to `location ~ \.php` block that resides
   on the outside with a different root that results in 404.
 *  Thread Starter [ansonl](https://wordpress.org/support/users/ansonl/)
 * (@ansonl)
 * [13 years ago](https://wordpress.org/support/topic/directory-access-using-nginx/#post-3758016)
 * Thanks for the help, after adding in the nested`~ \.php` block, the PHP files
   inside the `/api` directory work perfectly, however when accessing static text
   files, Nginx returns a nginx 404 error page instead of the wordpress error now.
   
   For example, accessing file located at `/api/newest.txt` returns 404 whereas 
   accessing `/api/newest.plist` serves the actual plist file which the browser 
   downloads.
 * Modified config following the pastebin example below _(I have removed the `try\
   _files` option for `/api`, is try\_file only necessary for PHP files? Adding `
   try\_files` doesn’t seem to make a difference._):
 *     ```
       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 {
                   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;
                       }
               }
   
               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;
               }
       }
       ```
   
 *  [Pothi Kalimuthu](https://wordpress.org/support/users/pothi/)
 * (@pothi)
 * [13 years ago](https://wordpress.org/support/topic/directory-access-using-nginx/#post-3758017)
 * You may have [proxy_intercept_errors](http://wiki.nginx.org/HttpProxyModule#proxy_intercept_errors)
   somewhere in your configuration. If that is not used, then you may have to check
   the error log to see how things work in real time to catch the error.
 * Alternatively, post your latest query in a separate thread. Someone else with
   the right knowledge may chip in.
 *  Thread Starter [ansonl](https://wordpress.org/support/users/ansonl/)
 * (@ansonl)
 * [13 years ago](https://wordpress.org/support/topic/directory-access-using-nginx/#post-3758020)
 * I posted on rtCamp’s forums and have gotten the problem partially solved for 
   those who are wondering.
 * [https://rtcamp.com/support/topic/accessing-non-wordpress-directory-in-website-root/#post-38522](https://rtcamp.com/support/topic/accessing-non-wordpress-directory-in-website-root/#post-38522)

Viewing 6 replies - 1 through 6 (of 6 total)

The topic ‘Directory access using Nginx’ is closed to new replies.

## Tags

 * [nginx](https://wordpress.org/support/topic-tag/nginx/)
 * [root](https://wordpress.org/support/topic-tag/root/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 6 replies
 * 2 participants
 * Last reply from: [ansonl](https://wordpress.org/support/users/ansonl/)
 * Last activity: [13 years ago](https://wordpress.org/support/topic/directory-access-using-nginx/#post-3758020)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
