1

I have my nginx home at /opt/nginx, inside there are site1 and mail folders, site1 has html folder that is a wordpress installation and mail is a webmail site, both of them must be proxied to php-fpm, site1/html works like a charm no problem at all.

I have the domain1.com and my server delivers site1/html content when domain1.com is requested.

What I want to do is, when domain1.com/mail is requested, serve the content of mail folder (sibling of site1). If I left a index.html file inside mail, when domail1.com/mail is requested, index.html is served to the client without problem but if I try to deliver mail/index.php, 404 error rise instead, what am I doing wrong? below my config:

/etc/nginx/conf.d/domain1.com.conf

server {
.
.
.

root /opt/nginx/site1/html;
index index.html index.php;

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

location /mail {
    root /opt/nginx/;
    try_files $uri $uri/mail mail/index.php;        
}

location ~ [^/]\.php(/|$) {
    # SECURITY : Zero day Exploit Protection
    try_files $uri =404;
    fastcgi_split_path_info ^(.+?\.php)(/.*)$;

    fastcgi_pass unix:/var/run/php-fpm.sock;
    fastcgi_index index.php;
    include fastcgi_params;
}
}
nighter
  • 125
  • 1
  • 2
  • 10
  • I've already tried this [link](http://stackoverflow.com/questions/27066463/nginx-alias-directive-not-working-with-php) – nighter Feb 21 '16 at 00:46

1 Answers1

1

You can use nested location blocks to invoke PHP scripts which are in a different document root.

Like this:

root /opt/nginx/site1/html;
index index.html index.php;

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

location ^~ /mail {
    root /opt/nginx;
    try_files $uri $uri/ /mail/index.php;        

    location ~ \.php$ {
        try_files $uri =404;    
        fastcgi_pass unix:/var/run/php-fpm.sock;
        include fastcgi_params;
    }

}

location ~ \.php$ {
    try_files $uri =404;
    fastcgi_pass unix:/var/run/php-fpm.sock;
    include fastcgi_params;
}

Notice the ^~ modifier which allows the nested PHP block to take precedence over the outer PHP block. I also removed the path info code which was not being used.

Richard Smith
  • 45,711
  • 6
  • 82
  • 81
  • Worked like a charm, as I didn't realize, I managed to bypass my problem doing a soft link: cd /opt/nginx/site1/html ln -s /opt/nginx/mail mail as mail now is inside /, no modification to config file was need so I left it as: root /opt/nginx/site1/html; index index.html index.php; location / { try_files $uri $uri/ /index.php?$args; } location ~ \.php$ { try_files $uri =404; fastcgi_pass unix:/var/run/php-fpm.sock; include fastcgi_params; } unfortunately doing so, I needed to ask my client for permission in order to add the soft link, now I can manage all in config file. – nighter Feb 22 '16 at 22:55