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

查看:76
本文介绍了使用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/ 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 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天全站免登陆