Featured image of post 一个端口同时支持http和https访问

一个端口同时支持http和https访问

前言

我们预设一个需求,就是某系统升级,客户要求网络策略不能变(即不能随意新增端口),在现有策略的情况下一个端口需要同时扩展支持http和https,并保持原来的代理的地址不变。

我们如何做实现?

方案一. 使用nginx的stream、 stream_ssl_preread模块

参考资料

简单来说就是ngx_stream_ssl_preread_module模块(1.11.5)允许从ClientHello(这个消息是客户端连接到服务器发送的第一条消息,其中就有ssl/tls的版本)消息中提取信息,而无需终止 SSL/TLS。我们可以提取这个信息利用它匹配不同的路由,从而满足上述需求。

准备工作

  • nginx版本1.11.5及以上

  • 由于stream和stream_ssl_preread模块非默认引入,需要在编译安装nginx时引入;

    编译时添加配置参数 –with-stream –with-stream_ssl_preread_module

配置stream

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
stream {
  upstream http_gw {
    # 8080端口是一个开启http的端口
    server  127.0.0.1:8080;
  }
  upstream https_gw {
    # 8443端口是一个开启https的端口
    server  127.0.0.1:8443;
  }
  # 根据不同的协议走不同的upstream
  map $ssl_preread_protocol $upstream{
    default http_gw;
    "TLSv1.0" https_gw;
    "TLSv1.1" https_gw;
    "TLSv1.2" https_gw;
    "TLSv1.3" https_gw;
  }
  server {
    listen 83;
    ssl_preread on;
    proxy_pass $upstream;
  }
}

简单nginx.conf示例

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
user  root;
worker_processes  8;
worker_rlimit_nofile  100000;
stream {
  upstream http_gw {
    server  127.0.0.1:8080;
  }
  upstream https_gw {
    server  127.0.0.1:8443;
  }
  map $ssl_preread_protocol $upstream{
    default http_gw;
    "TLSv1.0" https_gw;
    "TLSv1.1" https_gw;
    "TLSv1.2" https_gw;
    "TLSv1.3" https_gw;
  }

  server {
    listen 83;
    ssl_preread on;
    proxy_pass $upstream;
  }
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    upstream services1 {
        server  192.168.1.1:8080  weight=1;
        server  192.168.1.2:8080  weight=2;
    }

    server {
        listen       8080;
        listen       8443 ssl;
        server_name  your.domain.name;
        ssl_certificate      your.domain.name.pem;
        ssl_certificate_key  your.domain.name.key;
        location / {
            proxy_pass http://services1 ;
        }
    }

}

方案二. nginx强制将http重定向到https(不推荐使用)

  • 利用http请求https端口时的错误码497将请求重定向到https
  • 配置如下:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
server {
         listen       83 ssl;
         server_name  your.domain.name;
         ssl_certificate     your.domain.name.pem;
         ssl_certificate_key  your.domain.name.key;
         location / {
             proxy_pass http://xxxxx;
             error_page 497 https://$host:$server_port$request_uri;
         }
 }
  • 缺点:只支持浏览端的页面访问,对于接口调用会有莫名的跨域问题,一些网络框架默认也不支持访问重定向的接口