天天看點

使用NGINX作為HTTPS正向代理伺服器HTTP/HTTPS正向代理的分類為什麼正向代理處理HTTPS流量需要特殊處理?NGINX的解決方案總結

NGINX主要設計作為反向代理伺服器,但随着NGINX的發展,它同樣能作為正向代理的選項之一。正向代理本身并不複雜,而如何代理加密的HTTPS流量是正向代理需要解決的主要問題。本文将介紹利用NGINX來正向代理HTTPS流量兩種方案,及其使用場景和主要問題。

HTTP/HTTPS正向代理的分類

簡單介紹下正向代理的分類作為了解下文的背景知識:

按用戶端有無感覺的分類

  • 普通代理:在用戶端需要在浏覽器中或者系統環境變量手動設定代理的位址和端口。如squid,在用戶端指定squid伺服器IP和端口3128。
  • 透明代理:用戶端不需要做任何代理設定,“代理”這個角色對于用戶端是透明的。如企業網絡鍊路中的Web Gateway裝置。

按代理是否解密HTTPS的分類

  • 隧道代理 :也就是透傳代理。代理伺服器隻是在TCP協定上透傳HTTPS流量,對于其代理的流量的具體内容不解密不感覺。用戶端和其通路的目的伺服器做直接TLS/SSL互動。本文中讨論的NGINX代理方式屬于這種模式。
  • 中間人(MITM, Man-in-the-Middle)代理:代理伺服器解密HTTPS流量,對用戶端利用自簽名證書完成TLS/SSL握手,對目的伺服器端完成正常TLS互動。在用戶端-代理-伺服器的鍊路中建立兩段TLS/SSL會話。如 Charles ,簡單原理描述可以參考 文章

    注:這種情況用戶端在TLS握手階段實際上是拿到的代理伺服器自己的自簽名證書,證書鍊的驗證預設不成功,需要在用戶端信任代理自簽證書的Root CA憑證。是以過程中是用戶端有感的。如果要做成無感的透明代理,需要向用戶端推送自簽的Root CA憑證,在企業内部環境下是可實作的。

為什麼正向代理處理HTTPS流量需要特殊處理?

作為反向代理時,代理伺服器通常終結 (terminate) HTTPS加密流量,再轉發給後端執行個體。HTTPS流量的加解密和認證過程發生在用戶端和反向代理伺服器之間。

而作為正向代理在處理用戶端發過來的流量時,HTTP加密封裝在了TLS/SSL中,代理伺服器無法看到用戶端請求URL中想要通路的域名,如下圖。是以代理HTTPS流量,相比于HTTP,需要做一些特殊處理。

使用NGINX作為HTTPS正向代理伺服器HTTP/HTTPS正向代理的分類為什麼正向代理處理HTTPS流量需要特殊處理?NGINX的解決方案總結

NGINX的解決方案

根據前文中的分類方式,NGINX解決HTTPS代理的方式都屬于透傳(隧道)模式,即不解密不感覺上層流量。具體的方式有如下7層和4層的兩類解決方案。

HTTP CONNECT隧道 (7層解決方案)

曆史背景

早在1998年,也就是TLS還沒有正式誕生的SSL時代,主導SSL協定的Netscape公司就提出了關于利用web代理來tunneling SSL流量的

INTERNET-DRAFT

。其核心思想就是利用HTTP CONNECT請求在用戶端和代理之間建立一個HTTP CONNECT Tunnel,在CONNECT請求中需要指定用戶端需要通路的目的主機和端口。Draft中的原圖如下:

使用NGINX作為HTTPS正向代理伺服器HTTP/HTTPS正向代理的分類為什麼正向代理處理HTTPS流量需要特殊處理?NGINX的解決方案總結

整個過程可以參考HTTP權威指南中的圖:

  1. 用戶端給代理伺服器發送HTTP CONNECT請求。
  2. 代理伺服器利用HTTP CONNECT請求中的主機和端口與目的伺服器建立TCP連接配接。
  3. 代理伺服器給用戶端傳回HTTP 200響應。
  4. 用戶端和代理伺服器建立起HTTP CONNECT隧道,HTTPS流量到達代理伺服器後,直接通過TCP透傳給遠端目的伺服器。代理伺服器的角色是透傳HTTPS流量,并不需要解密HTTPS。
    使用NGINX作為HTTPS正向代理伺服器HTTP/HTTPS正向代理的分類為什麼正向代理處理HTTPS流量需要特殊處理?NGINX的解決方案總結

NGINX ngx_http_proxy_connect_module子產品

NGINX作為反向代理伺服器,官方一直沒有支援HTTP CONNECT方法。但是基于NGINX的子產品化、可擴充性好的特性,阿裡的@chobits提供了

ngx_http_proxy_connect_module

子產品,來支援HTTP CONNECT方法,進而讓NGINX可以擴充為正向代理。

環境搭建

以CentOS 7的環境為例。

1) 安裝

對于新安裝的環境,參考正常的

安裝步驟

和安裝這個子產品的步驟(

https://github.com/chobits/ngx_http_proxy_connect_module)

,把對應版本的patch打上之後,在configure的時候加上參數--add-module=/path/to/ngx_http_proxy_connect_module,示例如下:

./configure \
--user=www \
--group=www \
--prefix=/usr/local/nginx \
--with-http_ssl_module \
--with-http_stub_status_module \
--with-http_realip_module \
--with-threads \
--add-module=/root/src/ngx_http_proxy_connect_module           

對于已經安裝編譯安裝完的環境,需要加入以上子產品,步驟如下:

# 停止NGINX服務
# systemctl stop nginx
# 備份原執行檔案
# cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak
# 在源代碼路徑重新編譯
# cd /usr/local/src/nginx-1.16.0
./configure \
--user=www \
--group=www \
--prefix=/usr/local/nginx \
--with-http_ssl_module \
--with-http_stub_status_module \
--with-http_realip_module \
--with-threads \
--add-module=/root/src/ngx_http_proxy_connect_module
# make
# 不要make install
# 将新生成的可執行檔案拷貝覆寫原來的nginx執行檔案
# cp objs/nginx /usr/local/nginx/sbin/nginx
# /usr/bin/nginx -V
nginx version: nginx/1.16.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-36) (GCC)
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --user=www --group=www --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-threads --add-module=/root/src/ngx_http_proxy_connect_module           

2) nginx.conf檔案配置

server {
     listen  443;
    
     # dns resolver used by forward proxying
     resolver  114.114.114.114;

     # forward proxy for CONNECT request
     proxy_connect;
     proxy_connect_allow            443;
     proxy_connect_connect_timeout  10s;
     proxy_connect_read_timeout     10s;
     proxy_connect_send_timeout     10s;

     # forward proxy for non-CONNECT request
     location / {
         proxy_pass http://$host;
         proxy_set_header Host $host;
     }
 }           

使用場景

7層需要通過HTTP CONNECT來建立隧道,屬于用戶端有感覺的普通代理方式,需要在用戶端手動配置HTTP(S)代理伺服器IP和端口。在用戶端用curl 加-x參數通路如下:

# curl https://www.baidu.com -svo /dev/null -x 39.105.196.164:443
* About to connect() to proxy 39.105.196.164 port 443 (#0)
*   Trying 39.105.196.164...
* Connected to 39.105.196.164 (39.105.196.164) port 443 (#0)
* Establish HTTP proxy tunnel to www.baidu.com:443
> CONNECT www.baidu.com:443 HTTP/1.1
> Host: www.baidu.com:443
> User-Agent: curl/7.29.0
> Proxy-Connection: Keep-Alive
>
< HTTP/1.1 200 Connection Established
< Proxy-agent: nginx
<
* Proxy replied OK to CONNECT request
* Initializing NSS with certpath: sql:/etc/pki/nssdb
*   CAfile: /etc/pki/tls/certs/ca-bundle.crt
  CApath: none
* SSL connection using TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256
* Server certificate:
*     subject: CN=baidu.com,O="Beijing Baidu Netcom Science Technology Co., Ltd",OU=service operation department,L=beijing,ST=beijing,C=CN
...
> GET / HTTP/1.1
> User-Agent: curl/7.29.0
> Host: www.baidu.com
> Accept: */*
>
< HTTP/1.1 200 OK
...
{ [data not shown]           

從上面-v參數列印出的細節,可以看到用戶端先往代理伺服器39.105.196.164建立了HTTP CONNECT隧道,代理回複HTTP/1.1 200 Connection Established後就開始互動TLS/SSL握手和流量了。

NGINX stream (4層解決方案)

既然是使用透傳上層流量的方法,那可不可做成“4層代理”,對TCP/UDP以上的協定實作徹底的透傳呢?答案是可以的。NGINX官方從1.9.0版本開始支援

ngx_stream_core_module

子產品,子產品預設不build,需要configure時加上--with-stream選項來開啟。

問題

用NGINX stream在TCP層面上代理HTTPS流量肯定會遇到本文一開始提到的那個問題:代理伺服器無法擷取用戶端想要通路的目的域名。因為在TCP的層面擷取的資訊僅限于IP和端口層面,沒有任何機會拿到域名資訊。要拿到目的域名,必須要有拆上層封包擷取域名資訊的能力,是以NGINX stream的方式不是完全嚴格意義上的4層代理,還是要略微借助些上層能力。

ngx_stream_ssl_preread_module子產品

要在不解密的情況下拿到HTTPS流量通路的域名,隻有利用TLS/SSL握手的第一個Client Hello封包中的擴充位址SNI (Server Name Indication)來擷取。NGINX官方從1.11.5版本開始支援利用

ngx_stream_ssl_preread_module

子產品來獲得這個能力,子產品主要用于擷取Client Hello封包中的SNI和ALPN資訊。對于4層正向代理來說,從Client Hello封包中提取SNI的能力是至關重要的,否則NGINX stream的解決方案無法成立。同時這也帶來了一個限制,要求所有用戶端都需要在TLS/SSL握手中帶上SNI字段,否則NGINX stream代理完全沒辦法知道用戶端需要通路的目的域名。

,直接在configure的時候加上--with-stream,--with-stream_ssl_preread_module和--with-stream_ssl_module選項即可。示例如下:

./configure \
--user=www \
--group=www \
--prefix=/usr/local/nginx \
--with-http_ssl_module \
--with-http_stub_status_module \
--with-http_realip_module \
--with-threads \
--with-stream \
--with-stream_ssl_preread_module \
--with-stream_ssl_module           

對于已經安裝編譯安裝完的環境,需要加入以上3個與stream相關的子產品,步驟如下:

# 停止NGINX服務
# systemctl stop nginx
# 備份原執行檔案
# cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak
# 在源代碼路徑重新編譯
# cd /usr/local/src/nginx-1.16.0
# ./configure \
--user=www \
--group=www \
--prefix=/usr/local/nginx \
--with-http_ssl_module \
--with-http_stub_status_module \
--with-http_realip_module \
--with-threads \
--with-stream \
--with-stream_ssl_preread_module \
--with-stream_ssl_module
# make
# 不要make install
# 将新生成的可執行檔案拷貝覆寫原來的nginx執行檔案
# cp objs/nginx /usr/local/nginx/sbin/nginx
# nginx -V
nginx version: nginx/1.16.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-36) (GCC)
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --user=www --group=www --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-threads --with-stream --with-stream_ssl_preread_module --with-stream_ssl_module           

NGINX stream與HTTP不同,需要在stream塊中進行配置,但是指令參數與HTTP塊都是類似的,主要配置部分如下:

stream {
    resolver 114.114.114.114;
    server {
        listen 443;
        ssl_preread on;
        proxy_connect_timeout 5s;
        proxy_pass $ssl_preread_server_name:$server_port;
    }
}           

對于4層正向代理,NGINX對上層流量基本上是透傳,也不需要HTTP CONNECT來建立隧道。适合于透明代理的模式,比如将通路的域名利用DNS解定向到代理伺服器。我們可以通過在用戶端綁定/etc/hosts來模拟。

在用戶端:

cat /etc/hosts
...
# 把域名www.baidu.com綁定到正向代理伺服器39.105.196.164
39.105.196.164 www.baidu.com

# 正常利用curl來通路www.baidu.com即可。
# curl https://www.baidu.com -svo /dev/null
* About to connect() to www.baidu.com port 443 (#0)
*   Trying 39.105.196.164...
* Connected to www.baidu.com (39.105.196.164) port 443 (#0)
* Initializing NSS with certpath: sql:/etc/pki/nssdb
*   CAfile: /etc/pki/tls/certs/ca-bundle.crt
  CApath: none
* SSL connection using TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256
* Server certificate:
*     subject: CN=baidu.com,O="Beijing Baidu Netcom Science Technology Co., Ltd",OU=service operation department,L=beijing,ST=beijing,C=CN
*     start date: 5月 09 01:22:02 2019 GMT
*     expire date: 6月 25 05:31:02 2020 GMT
*     common name: baidu.com
*     issuer: CN=GlobalSign Organization Validation CA - SHA256 - G2,O=GlobalSign nv-sa,C=BE
> GET / HTTP/1.1
> User-Agent: curl/7.29.0
> Host: www.baidu.com
> Accept: */*
>
< HTTP/1.1 200 OK
< Accept-Ranges: bytes
< Cache-Control: private, no-cache, no-store, proxy-revalidate, no-transform
< Connection: Keep-Alive
< Content-Length: 2443
< Content-Type: text/html
< Date: Fri, 21 Jun 2019 05:46:07 GMT
< Etag: "5886041d-98b"
< Last-Modified: Mon, 23 Jan 2017 13:24:45 GMT
< Pragma: no-cache
< Server: bfe/1.0.8.18
< Set-Cookie: BDORZ=27315; max-age=86400; domain=.baidu.com; path=/
<
{ [data not shown]
* Connection #0 to host www.baidu.com left intact           

常見問題

1) 用戶端手動設定代理導緻通路不成功

4層正向代理是透傳上層HTTPS流量,不需要HTTP CONNECT來建立隧道,也就是說不需要用戶端設定HTTP(S)代理。如果我們在用戶端手動設定HTTP(s)代理是否能通路成功呢? 我們可以用curl -x來設定代理為這個正向伺服器通路測試,看看結果:

# curl https://www.baidu.com -svo /dev/null -x 39.105.196.164:443
* About to connect() to proxy 39.105.196.164 port 443 (#0)
*   Trying 39.105.196.164...
* Connected to 39.105.196.164 (39.105.196.164) port 443 (#0)
* Establish HTTP proxy tunnel to www.baidu.com:443
> CONNECT www.baidu.com:443 HTTP/1.1
> Host: www.baidu.com:443
> User-Agent: curl/7.29.0
> Proxy-Connection: Keep-Alive
>
* Proxy CONNECT aborted
* Connection #0 to host 39.105.196.164 left intact           

可以看到用戶端試圖于正向NGINX前建立HTTP CONNECT tunnel,但是由于NGINX是透傳,是以把CONNECT請求直接轉發給了目的伺服器。目的伺服器不接受CONNECT方法,是以最終出現"Proxy CONNECT aborted",導緻通路不成功。

2) 用戶端沒有帶SNI導緻通路不成功

上文提到用NGINX stream做正向代理的關鍵因素之一是利用ngx_stream_ssl_preread_module提取出Client Hello中的SNI字段。如果用戶端用戶端不攜帶SNI字段,會造成代理伺服器無法獲知目的域名的情況,導緻通路不成功。

在透明代理模式下(用手動綁定hosts的方式模拟),我們可以在用戶端用openssl來模拟:

# openssl s_client -connect www.baidu.com:443 -msg
CONNECTED(00000003)
>>> TLS 1.2  [length 0005]
    16 03 01 01 1c
>>> TLS 1.2 Handshake [length 011c], ClientHello
    01 00 01 18 03 03 6b 2e 75 86 52 6c d5 a5 80 d7
    a4 61 65 6d 72 53 33 fb 33 f0 43 a3 aa c2 4a e3
    47 84 9f 69 8b d6 00 00 ac c0 30 c0 2c c0 28 c0
    24 c0 14 c0 0a 00 a5 00 a3 00 a1 00 9f 00 6b 00
    6a 00 69 00 68 00 39 00 38 00 37 00 36 00 88 00
    87 00 86 00 85 c0 32 c0 2e c0 2a c0 26 c0 0f c0
    05 00 9d 00 3d 00 35 00 84 c0 2f c0 2b c0 27 c0
    23 c0 13 c0 09 00 a4 00 a2 00 a0 00 9e 00 67 00
    40 00 3f 00 3e 00 33 00 32 00 31 00 30 00 9a 00
    99 00 98 00 97 00 45 00 44 00 43 00 42 c0 31 c0
    2d c0 29 c0 25 c0 0e c0 04 00 9c 00 3c 00 2f 00
    96 00 41 c0 12 c0 08 00 16 00 13 00 10 00 0d c0
    0d c0 03 00 0a 00 07 c0 11 c0 07 c0 0c c0 02 00
    05 00 04 00 ff 01 00 00 43 00 0b 00 04 03 00 01
    02 00 0a 00 0a 00 08 00 17 00 19 00 18 00 16 00
    23 00 00 00 0d 00 20 00 1e 06 01 06 02 06 03 05
    01 05 02 05 03 04 01 04 02 04 03 03 01 03 02 03
    03 02 01 02 02 02 03 00 0f 00 01 01
140285606590352:error:140790E5:SSL routines:ssl23_write:ssl handshake failure:s23_lib.c:177:
---
no peer certificate available
---
No client certificate CA names sent
---
SSL handshake has read 0 bytes and written 289 bytes
...           

openssl s_client預設不帶SNI,可以看到上面的請求在TLS/SSL握手階段,發出Client Hello後就結束了。因為代理伺服器不知道要把Client Hello往哪個目的域名轉發。

如果用openssl帶servername參數來指定SNI,則可以正常通路成功,指令如下:

# openssl s_client -connect www.baidu.com:443 -servername www.baidu.com           

總結

本文總結了NGINX利用HTTP CONNECT隧道和NGINX stream兩種方式做HTTPS正向代理的原理,環境搭建,使用場景和主要問題,希望給大家在做各種場景的正向代理時提供參考。

繼續閱讀