简体   繁体   中英

nginx redirection to sibling folder and serve php not working

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM