• 0

NGINX subfolder to different root with rewrite rule and PHP not working



I've been trying to add a new rule in my nginx server configuration but just can't get it to work.  I've stripped out the stuff that doesn't matter (SSL certificate locations, etc), and this is what i have:


server {
    root /var/www/stage/stage.domain.com;
    index index.php;
    server_name stage.domain.com;
    location /subfolder {
        root /var/www/stage/stage.domain2.com;
        try_files $uri $uri/ =404;
        if (!-f $uri){
                set $rule_2 1$rule_2;
        if ($rule_2 = "1"){
                rewrite /. /index.php last;
        location ~ \.php$ {
            include snippets/fastcgi-php.conf;
            fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
    location / {
        try_files $uri $uri/ =404;
    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;


The part I'm adding is the "location /subfolder {" part.


Basically the site already exists, but doesn't have any rewrite happening at all.  This new subfolder, I need to load from a different root, and have a rewrite rule to always load the root index.php file no matter what.


Every thing I've tried results in one of three things happening:

1. Loads the index.php file, but doesn't run PHP (downloads the file)

2. Runs php, but doesn't load the rewrite rule, so returns a 404 error

3. Ignores everything, and loads the index.php file from the base domain "location /" block.


Any ideas?

Link to post
Share on other sites

0 answers to this question

Recommended Posts

There have been no answers to this question yet

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    No registered users viewing this page.