开发者

Rewrite configuration issue in a Nginx server

开发者 https://www.devze.com 2023-04-06 00:40 出处:网络
I\'m configuring an Nginx server with both http and https services. I\'m trying to achive the following configuration:

I'm configuring an Nginx server with both http and https services. I'm trying to achive the following configuration:

redirect every page to HTTPS, except for th开发者_运维问答e home page

In my "http" server configuration, I have already the second rewrite condition working, but I cannot find the way to write the first.

location = / {
  what goes here??? 
 }

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

Ideas?


Zenofo's answer should mostly work (just needs the regex "!~*" instead) but will redirect requests that include the name of the home page along with the others.

Using "$uri" in place of "$request_uri" and spelling out the home page file name in the regex gets around this.

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

    # Process homepage requests
    ....

}

If running php where everything goes through index.php (front end controller) then you can use

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

    # Process homepage requests
    ....

}


using $request_uri,like this: (I haven't tested)

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号