使用 nginx 重定向到 SSL [英] Redirecting to SSL using nginx
问题描述
我在同一台主机上有 http://和 https://,如下所示:
I have http:// and https:// on the same host like the following:
server {
listen 80;
listen 443 ssl;
...
...
}
我需要做的是将访问我的商店的用户重定向到 https://.问题是我有很多语言:
What I need to do is redirecting users who access my shop to https://. The problem is I have many languages:
https://mydomain.com/en/shophttps://mydomain.com/fr/shop等等...
https://mydomain.com/en/shop https://mydomain.com/fr/shop etc...
我试过了,没有用 (nginx: 配置文件/etc/nginx/nginx.conf 测试失败)
:
if ($server_port = 80) {
location (en|fr)/shop {
rewrite ^ https://$host$request_uri permanent;
}
}
推荐答案
为了使用正则表达式进行匹配 location
s,你需要在表达式前加上 ~
或 ~*
:
In order to use regular expressions for matching location
s, you need to prefix the expression with either ~
or ~*
:
if ($server_port = 80) {
location ~ (en|fr)/shop {
rewrite ^ https://$host$request_uri permanent;
}
}
来自文档:
要使用正则表达式,必须使用前缀:
To use regular expressions, you must use a prefix:
"~"
区分大小写匹配~*"
不区分大小写匹配
"~"
for case sensitive matching"~*"
for case insensitive matching
由于 nginx 不允许 location
块嵌套在 if
块内,请尝试以下配置:
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;
}
这篇关于使用 nginx 重定向到 SSL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!