Support » Fixing WordPress » 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.

    The Nginx configuration has the root set to /blog and 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, 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)
  • Please post your Nginx conf, to see what could be fixed in it.

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

    Please check out the modified code.

    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.

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

    You may have 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.

    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

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Directory access using Nginx’ is closed to new replies.