重写Nginx服务器中的配置问题

时间:2011-09-19 10:43:37

标签: nginx rewrite

我正在使用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;
 }

想法?

2 个答案:

答案 0 :(得分:0)

使用$request_uri,如下所示:(我还没有测试过)

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

答案 1 :(得分:0)

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
    ....

}
相关问题