加入收藏 | 设为首页 | 会员中心 | 我要投稿 孝感站长网 (https://www.0712zz.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长资讯 > 外闻 > 正文

使用Nginx作为HTTPS正向代理服务器

发布时间:2019-07-11 18:21:39 所属栏目:外闻 来源:今日头条
导读:NGINX主要设计作为反向代理服务器,但随着NGINX的发展,它同样能作为正向代理的选项之一。正向代理本身并不复杂,而如何代理加密的HTTPS流量是正向代理需要解决的主要问题。本文将介绍利用NGINX来正向代理HTTPS流量两种方案,及其使用场景和主要问题。 HTT

对于新安装的环境,参考正常的安装步骤,直接在configure的时候加上--with-stream,--with-stream_ssl_preread_module和--with-stream_ssl_module选项即可。示例如下:

  1. ./configure  
  2. --user=www  
  3. --group=www  
  4. --prefix=/usr/local/nginx  
  5. --with-http_ssl_module  
  6. --with-http_stub_status_module  
  7. --with-http_realip_module  
  8. --with-threads  
  9. --with-stream  
  10. --with-stream_ssl_preread_module  
  11. --with-stream_ssl_module 

对于已经安装编译安装完的环境,需要加入以上3个与stream相关的模块,步骤如下:

  1. # 停止NGINX服务 
  2. # systemctl stop nginx 
  3. # 备份原执行文件 
  4. # cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak 
  5. # 在源代码路径重新编译 
  6. # cd /usr/local/src/nginx-1.16.0 
  7. # ./configure  
  8. --user=www  
  9. --group=www  
  10. --prefix=/usr/local/nginx  
  11. --with-http_ssl_module  
  12. --with-http_stub_status_module  
  13. --with-http_realip_module  
  14. --with-threads  
  15. --with-stream  
  16. --with-stream_ssl_preread_module  
  17. --with-stream_ssl_module 
  18. # make 
  19. # 不要make install 
  20. # 将新生成的可执行文件拷贝覆盖原来的nginx执行文件 
  21. # cp objs/nginx /usr/local/nginx/sbin/nginx 
  22. # nginx -V 
  23. nginx version: nginx/1.16.0 
  24. built by gcc 4.8.5 20150623 (Red Hat 4.8.5-36) (GCC) 
  25. built with OpenSSL 1.0.2k-fips 26 Jan 2017 
  26. TLS SNI support enabled 
  27. 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 

2) nginx.conf文件配置

NGINX stream与HTTP不同,需要在stream块中进行配置,但是指令参数与HTTP块都是类似的,主要配置部分如下:

  1. stream { 
  2.  resolver 114.114.114.114; 
  3.  server { 
  4.  listen 443; 
  5.  ssl_preread on; 
  6.  proxy_connect_timeout 5s; 
  7.  proxy_pass $ssl_preread_server_name:$server_port; 
  8.  } 

使用场景

对于4层正向代理,NGINX对上层流量基本上是透传,也不需要HTTP CONNECT来建立隧道。适合于透明代理的模式,比如将访问的域名利用DNS解定向到代理服务器。我们可以通过在客户端绑定/etc/hosts来模拟。

在客户端:

  1. cat /etc/hosts 
  2. ... 
  3. # 把域名www.baidu.com绑定到正向代理服务器39.105.196.164 
  4. 39.105.196.164 www.baidu.com 
  5. # 正常利用curl来访问www.baidu.com即可。 
  6. # curl https://www.baidu.com -svo /dev/null 
  7. * About to connect() to www.baidu.com port 443 (#0) 
  8. * Trying 39.105.196.164... 
  9. * Connected to www.baidu.com (39.105.196.164) port 443 (#0) 
  10. * Initializing NSS with certpath: sql:/etc/pki/nssdb 
  11. * CAfile: /etc/pki/tls/certs/ca-bundle.crt 
  12.  CApath: none 
  13. * SSL connection using TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 
  14. * Server certificate: 
  15. * subject: CN=baidu.com,O="Beijing Baidu Netcom Science Technology Co., Ltd",OU=service operation department,L=beijing,ST=beijing,C=CN 
  16. * start date: 5月 09 01:22:02 2019 GMT 
  17. * expire date: 6月 25 05:31:02 2020 GMT 
  18. * common name: baidu.com 
  19. * issuer: CN=GlobalSign Organization Validation CA - SHA256 - G2,O=GlobalSign nv-sa,C=BE 
  20. > GET / HTTP/1.1 
  21. > User-Agent: curl/7.29.0 
  22. > Host: www.baidu.com 
  23. > Accept: */* 
  24. < HTTP/1.1 200 OK 
  25. < Accept-Ranges: bytes 
  26. < Cache-Control: private, no-cache, no-store, proxy-revalidate, no-transform 
  27. < Connection: Keep-Alive 
  28. < Content-Length: 2443 
  29. < Content-Type: text/html 
  30. < Date: Fri, 21 Jun 2019 05:46:07 GMT 
  31. < Etag: "5886041d-98b" 
  32. < Last-Modified: Mon, 23 Jan 2017 13:24:45 GMT 
  33. < Pragma: no-cache 
  34. < Server: bfe/1.0.8.18 
  35. < Set-Cookie: BDORZ=27315; max-age=86400; domain=.baidu.com; path=/ 
  36. { [data not shown] 
  37. * Connection #0 to host www.baidu.com left intact 

常见问题

1) 客户端手动设置代理导致访问不成功

(编辑:孝感站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

热点阅读