使用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/ zh_CN /shop https://mydomain.com/ fr /shop 等等...
https://mydomain.com/en/shop https://mydomain.com/fr/shop etc...
我尝试了此操作,但没有成功(nginx: configuration file /etc/nginx/nginx.conf test failed)
:
I tried this and it didn't work (nginx: configuration file /etc/nginx/nginx.conf test failed)
:
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屋!