使用 nginx 重定向到 SSL [英] Redirecting to SSL using nginx

查看:39
本文介绍了使用 nginx 重定向到 SSL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在同一台主机上有 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;
    }
}

推荐答案

为了使用正则表达式进行匹配 locations,你需要在表达式前加上 ~~*:

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;
    }
}

来自文档:

要使用正则表达式,必须使用前缀:

To use regular expressions, you must use a prefix:

  1. "~" 区分大小写匹配
  2. ~*" 不区分大小写匹配
  1. "~" for case sensitive matching
  2. "~*" 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆