【问题标题】:Nginx multiple root redirect issueNginx 多根重定向问题
【发布时间】:2017-09-02 17:20:02
【问题描述】:

我的以下配置运行良好:

root /var/www/en;
location / {
   try_files $uri $uri/ /index.php?$args;
}

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

location ~ \.php(/|\?|$) {
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME 
        $document_root$fastcgi_script_name;
        fastcgi_param COCKPIT_URL_REWRITE On;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/run/php/php7.0-fpm.sock;
        fastcgi_index index.php;
}

这会将所有请求重定向到 index.php 和 admin/index.php 以用于管理后端。 现在我需要将不同语言的网站副本放到具有相同子目录重定向规则的子目录中。这实际上是一个带有单独根文件夹的单独项目。我正在这样做:

location ~ ^/de {
   root /var/www/de;
   try_files $uri $uri/ /de/index.php?$args;
}


location ~ ^/de/admin {
   root /var/www/de;
   try_files $uri $uri/ /de/admin/index.phpd;
}

但是,这会导致以下 nginx 错误:

rewrite or internal redirection cycle while internally redirecting to "/de/index.php"

如何解决这个问题?

谢谢 ;)

更新:

通过将root 更改为alias,我设法使静态文件正常工作。所以,现在我可以访问静态文件 site.com/de/test.txt。但是,当我尝试访问 site.com/de/ 时,php 处理程序无法正常工作,浏览器会尝试下载 php 文件。我将 php 处理程序放在新的位置块中,但它仍然无法正常工作。

【问题讨论】:

    标签: redirect nginx location


    【解决方案1】:

    试试这个

    location ~ \.php(/|\?|$) {
    
      location /de {
         root /var/www/de;
         try_files $uri $uri/ /de/index.php?$args;
      }
    
      location /de/admin {
         root /var/www/de;
         try_files $uri $uri/ /de/admin/index.php;
      }
    
      include fastcgi_params;
      fastcgi_param SCRIPT_FILENAME 
      $document_root$fastcgi_script_name;
      fastcgi_param COCKPIT_URL_REWRITE On;
      fastcgi_split_path_info ^(.+\.php)(/.+)$;
      fastcgi_pass unix:/run/php/php7.0-fpm.sock;
      fastcgi_index index.php;
    }
    

    【讨论】:

    • 不幸的是,这不起作用。 Nginx 只是无法启动。此外,您要在 php 位置中添加它。所以,静态文件无论如何都不会工作。
    • 我猜这与位置匹配顺序有关。我阅读了文档,尝试了一些变体,但没有找到解决方案。如果您进一步研究这个问题,我将不胜感激。提前致谢。
    猜你喜欢
    • 2021-05-01
    • 2020-10-21
    • 2018-03-29
    • 2013-03-13
    • 2014-06-13
    • 1970-01-01
    • 1970-01-01
    • 2014-09-29
    • 1970-01-01
    相关资源
    最近更新 更多