代码之家  ›  专栏  ›  技术社区  ›  async_soul

Nginx try\u文件不适用于默认索引。html

  •  3
  • async_soul  · 技术社区  · 6 年前

    我正在使用nginx的$geoip\u country\u代码模块基于IP重定向用户。我的配置代码如下所示-

    server {
          listen 80;
          gzip on;
          server_name example.com;
          root html/example;
          location / {
            index index.html;
            try_files /$geoip_country_code/index.html /index.html;
          }
          location ~* \.(gif|jpg|jpeg|png|js|css)$ { }
      }
    

    想法是根据我已本地化的国家重定向用户,否则用户将转到默认索引,即其他所有人的通用索引。当我在本国的浏览器中打开它时,它可以完美地工作,因为我已经为它进行了本地化。但从其他国家/地区打开时,会显示内部服务器错误。它不会成为默认的索引页。

    有人能告诉我我做错了什么吗?

    1 回复  |  直到 6 年前
        1
  •  2
  •   Richard Smith    6 年前

    的最后一个元素 try_files 语句是默认操作,它是URI或响应代码。这个 /index.html 开始搜索新的 location 处理请求,并在开始时返回,因此您有一个重定向循环。

    您可以通过 /索引。html 文件 而不是术语。例如:

    try_files /$geoip_country_code/index.html /index.html =404;
    

    这个 =404 从未采取行动,因为 /索引。html 始终存在。

    就我个人而言,我会使用一种通用的解决方案:

    try_files /$geoip_country_code$uri $uri /index.html;
    

    看见 this document 更多信息。