美文网首页devops
Nginx无Https证书反向代理

Nginx无Https证书反向代理

作者: LoWang | 来源:发表于2019-07-15 09:47 被阅读0次

起因

在我们托管服务器中,有闲置服务器需要给子公司使用,在内网为其搭建了虚拟服务器,通过公网Nginx反向代理的内网。内网的机器也有nginx,并使用https协议。

发现问题

  • 在公网Nginx发现代理的配置过程中,发现如果不开启ssl配置,那么浏览器访问,连接直接被重置,如果开启ssl,那么需要证书。那么说明,nginx默认的https配置块无法支持7层的https转发,因为浏览器发出的握手协议是和公网Nginx进行的,此时公网Nginx并没有开启ssl,所以报错。
  • 如果公网配置ssl,那么证书使用自签,将会导致证书不被信任,而要客户获取证书,存在安全风险,所以两者皆不行。

SNI侦测

  • SNI(Server Name Indication)是为了解决一个服务器使用多个域名和证书的SSL/TLS扩展。一句话简述它的工作原理就是,在连接到服务器建立SSL链接之前先发送要访问站点的域名(Hostname),这样服务器根据这个域名返回一个合适的证书。目前,大多数操作系统和浏览器都已经很好地支持SNI扩展,OpenSSL 0.9.8已经内置这一功能,新版的nginx也支持SNI。

Nginx的SNI

  • 默认,新版的Nginx已经支持SNI,所以一个nginx下可以代理多个不同的域名。但是那是基于ssl配置在nginx下。如果让nginx代理内部的https,此法不行。

Nginx的Stream

  • 不知道从何时开始,Nginx也支持了TCP转发,也就是OSI中的第四层转发。SSL是依靠TCP传输,那么只需要把TCP上层的数据剥离出来,封装后转发给代理的443端口,然后将代理服务器的返回数据剥离出来,在通过公网ip返回,那么就相当于浏览器在直接和内网的443端口通信。

Nginx的ssl_preread

  • 不知道又从何开始,Nginx支持了ssl的preread,允许提取所述信息的ClientHello而不终止SSL / TLS。例如,服务器名称通过请求的消息SNI。该模块不是默认生成的,它应该使用--with-stream_ssl_preread_module配置参数启用。

nginx.conf配置

worker_processes  1;
events {
    worker_connections  1024;
}

pid /var/run/nginx.pid;
stream {
  log_format proxy '$proxy_protocol_addr $remote_addr [$time_local] '
    '$protocol $status $bytes_sent $bytes_received '
    '$session_time "$upstream_addr" '
    '"$upstream_bytes_sent" "$upstream_bytes_received" "$upstream_connect_time"';

  access_log /usr/local/nginx/logs/access.log proxy;
  error_log /usr/local/nginx/logs/error.log info;

  map_hash_bucket_size 64;
  
  #https://cloud.tencent.com/developer/section/1259623
  map $ssl_preread_server_name $backend_pool {
    www.xuankejia.cn server_cn;
    www.xuankejia.com server_com;
    ~*.xuankejia.cn server_cn; #wildcard
    ~*.xuankejia.com server_com; #wildcard
    default server_baidu;
  }

  upstream server_cn{
    server 192.168.1.15:443;
  }

  upstream server_com{
    server 172.16.0.59:443;
  }
  
  upstream server_baidu{
    server 127.0.0.1:443;
  }
  
  server{
    listen 443;
    #https://cloud.tencent.com/developer/section/1259675
    ssl_preread on;
    proxy_pass $backend_pool;
    proxy_connect_timeout 15s;
    proxy_timeout 15s;
    proxy_next_upstream_timeout 15s;
  }
}

Nginx With stream_ssl_preread_module


centos>

yum -y install pcre-devel

check wether install or not:
dpkg -l | grep zlib

ubuntu>

gcc g++:
sudo apt-get install build-essential
sudo apt-get install libtool

ssl:
sudo apt-get install openssl libssl-dev

pcre:
sudo apt-get install libpcre3 libpcre3-dev

zlib:
sudo apt-get install zlib1g-dev

make -j4

> ------------

./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


> --------------

BigVerion:

./configure --with-debug --prefix= --conf-path=conf/nginx.conf \
--pid-path=logs/nginx.pid --http-log-path=logs/access.log \
--error-log-path=logs/error.log --sbin-path=nginx.exe \
--http-client-body-temp-path=temp/client_body_temp \
--http-proxy-temp-path=temp/proxy_temp \
--http-fastcgi-temp-path=temp/fastcgi_temp \
--http-scgi-temp-path=temp/scgi_temp \
--http-uwsgi-temp-path=temp/uwsgi_temp --with-http_v2_module \
--with-http_realip_module --with-http_addition_module \
--with-http_sub_module --with-http_dav_module \
--with-http_stub_status_module --with-http_flv_module \
--with-http_mp4_module --with-http_gunzip_module \
--with-http_gzip_static_module --with-http_auth_request_module \
--with-http_random_index_module --with-http_secure_link_module \
--with-http_slice_module --with-mail --with-stream \
--with-http_ssl_module --with-mail_ssl_module --with-stream_ssl_module \
--with-threads --with-stream_ssl_preread_module


相关文章

网友评论

    本文标题:Nginx无Https证书反向代理

    本文链接:https://www.haomeiwen.com/subject/smsgkctx.html