【问题标题】:nginx redirection to sibling folder and serve php not workingnginx重定向到同级文件夹并服务php不起作用
【发布时间】:2016-02-21 00:40:36
【问题描述】:

我的 nginx 家在 /opt/nginx,里面有 site1ma​​il 文件夹,site1html 文件夹是 wordpress 安装和 ma​​il 是一个 webmail 站点,它们都必须代理到 php-fpm, site1/html 完全没有问题。

我有 domain1.com,当请求 domain1.com 时,我的服务器会提供 site1/html 内容。

我想要做的是,当 domain1.com/mail 被请求时,提供 ma​​il 文件夹的内容(site1)。如果我在邮件中留下 index.html 文件,当请求 domail1.com/mail 时,index.html 毫无问题地提供给客户端,但如果我尝试发送 ma​​il/index.php404 错误反而上升,我在做什么错误的?在我的配置下方:

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

【问题讨论】:

  • 我已经尝试过这个link

标签: php nginx


【解决方案1】:

您可以使用嵌套的位置块来调用位于不同文档根目录中的 PHP 脚本。

像这样:

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

注意^~ 修饰符,它允许嵌套的PHP 块优先于外部的PHP 块。我还删除了未使用的路径信息代码。

【讨论】:

  • 工作就像一个魅力,因为我没有意识到,我设法绕过我的问题做一个软链接: cd /opt/nginx/site1/html ln -s /opt/nginx/mail mail由于邮件现在在 / 中,因此不需要修改配置文件,所以我将其保留为:root /opt/nginx/site1/html;索引 index.html 索引.php;位置 / { try_files $uri $uri/ /index.php?$args; } 位置 ~ \.php$ { try_files $uri =404; fastcgi_pass unix:/var/run/php-fpm.sock;包括 fastcgi_params;不幸的是,我需要征求我的客户的许可才能添加软链接,现在我可以在配置文件中管理所有内容。
猜你喜欢
  • 1970-01-01
  • 2011-01-25
  • 2017-06-19
  • 2021-04-07
  • 1970-01-01
  • 2013-03-07
  • 1970-01-01
  • 2014-08-17
  • 2017-03-09
相关资源
最近更新 更多