3

I'm having some issues getting a subdirectory working on my nginx server.

I'm using nginx to serve a wordpress installation as the web root, and trying to run an additional php application at a subdirectory. Wordpress runs fine, but I cannot for the life of me get the application to run in the subdirectory without a 404, 403, or "No input file specified." error with various configurations. I'm sure there is something obvious, but I can't seem to figure it out!

Here is the relevant config:

Any help would be greatly appreciated!

server {
listen       myserver.edu:8081;                
server_name  myserver.edu:8081;           

try_files $uri $uri/ /index.php;


location / {
    root /path/to/nginx/html/wordpress;
    index index.php;
}

location /stacks {
    alias /another/path/to/usr/local/share/stacks/php;
    index index.php;
}

location ~ \.php$ {
    set $php_root /path/to/nginx/html/wordpress;
    include        fastcgi_params;
    fastcgi_pass   localhost:8082;
    fastcgi_param  SCRIPT_FILENAME  $php_root$fastcgi_script_name;
    }

location ~ \stacks.php$ {
    set $php_root /another/path/to/usr/local/share/stacks/php;
    include        fastcgi_params;
    fastcgi_pass   localhost:8082;
    fastcgi_param  SCRIPT_FILENAME  $php_root$fastcgi_script_name;
    }

2 Answers 2

5

I don't know how to do it using your alias and setting $php_root. I do know how to fix it if you make a symbolic link from the external folder into your wordpress-rootdirectory.

So using the terminal you make a symbolic link so that your stacks-subdirectory is an actual subdirectory:

 ln -s /another/path/to/usr/local/share/stacks/php /path/to/nginx/html/wordpress/stacks

As an nginx-config I would use

server {
    listen       myserver.edu:8081;                
    server_name  myserver.edu:8081;

    root /path/to/nginx/html/wordpress;
    index index.php;       

    location / {    
        try_files $uri $uri/ /index.php;
    }

    location /stacks {
        try_files $uri $uri/ /stacks/index.php;
    }

    location ~ \.php$ {
        fastcgi_pass   localhost:8082;
        include        fastcgi_params;
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Awesome! That totally worked! And much simpler. Any security concerns with doing it this way?
I would say that if your wordpress installation got hacked, they will be able to acces your other website as well and vice versa. But if you get hacked, you're screwed anyway. I wouldn't know why this would be more insecure than your previous attempt.
cool, thanks for the thoughts. i didn't think the previous was particularly secure, but I'm new to all of this so I thought I would ask!
1

Comment out 'try_files'. Do the sub directories start to work then? Perhaps it is processed before the 'location' directives are considered. If that's the case, then move the 'try_files' into the block for 'location /'.

I think that's a better place for 'try_files' anyway. In the current configuration, it looks like requests for files that don't exist will all be sent to Wordpress, even if they are in the 'stacks' directory.

1 Comment

Tried, but didn't work. Thanks for your suggestion, though. I had come across this possibility before.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.