Redirecting to SSL using nginx

前端 未结 5 1696
傲寒
傲寒 2020-12-08 16:29

I have http:// and https:// on the same host like the following:

server {

    listen   80;
    listen   443 ssl;

    ...
    ...
}

What I

5条回答
  •  独厮守ぢ
    2020-12-08 16:59

    In order to use regular expressions for matching locations, you need to prefix the expression with either ~ or ~*:

    if ($server_port = 80) {
        location ~ (en|fr)/shop {
            rewrite ^ https://$host$request_uri permanent;
        }
    }
    

    From the documentation:

    To use regular expressions, you must use a prefix:

    1. "~" for case sensitive matching
    2. "~*" for case insensitive matching

    Since nginx does't allow location blocks to be nested inside of if blocks, try the following configuration:

    if ($server_port = 80) {
        rewrite ^/(en|fr)/shop https://$host$request_uri permanent;
    }
    

提交回复
热议问题