【问题标题】:Rewrite configuration issue in a Nginx server重写 Nginx 服务器中的配置问题
【发布时间】:2011-09-24 13:52:00
【问题描述】:

我正在使用 http 和 https 服务配置 Nginx 服务器。我正在尝试实现以下配置:

redirect every page to HTTPS, except for the home page

在我的“http”服务器配置中,我已经有第二个重写条件工作,但我找不到编写第一个的方法。

location = / {
  what goes here??? 
 }

location / {
  rewrite ^(.*) https://mydomain.com$1 permanent;
 }

想法?

【问题讨论】:

    标签: nginx rewrite


    【解决方案1】:

    Zenofo 的答案应该大部分都有效(只需要正则表达式“!~*”),但会重定向包含主页名称的请求以及其他请求。

    使用“$uri”代替“$request_uri”并在正则表达式中拼出主页文件名可以解决这个问题。

    location / {
        if ($uri !~* ^/index.html)
        {
          # Redirect non home page requests
          rewrite ^ https://$host$request_uri? permanent;
        }
    
        # Process homepage requests
        ....
    
    }
    

    如果运行 php,一切都通过 index.php(前端控制器),那么你可以使用

    location / {
        if ($uri !~* ^/index.php$)
        {
          # Redirect non home page requests
          rewrite ^ https://$host$request_uri? permanent;
        }
    
        # Process homepage requests
        ....
    
    }
    

    【讨论】:

      【解决方案2】:

      使用$request_uri,像这样:(我没有测试过)

      location / {
          if ($request_uri != ^/$)
          {
            rewrite ^(.*) https://mydomain.com$1 permanent;
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-05-12
        • 2022-11-23
        • 2010-10-19
        • 2010-11-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多