Nginx之正向代理與反向代理進階方式(支持https)
在【Nginx之正向代理與反向代理】一文中我們實現了將Nginx服務器作為正向代理服務器和反向代理服務器,但美中不足的是僅支持http協(xié)議,不支持https協(xié)議。
我們先看看看http和https的區(qū)別:
- http協(xié)議:協(xié)議以明文方式發(fā)送數據,不提供任何方式的數據加密。不適合傳輸一些敏感信息,例如密碼。其使用的端口是
80
。 - https協(xié)議:在http協(xié)議的基礎上,加入了
SSL(Secure Sockets Layer)
,用于對數據進行加密。其使用的端口為443
。
現在,我們要完成Nginx對https協(xié)議的支持。
1.Nginx正向代理(http)
我們來回顧一下Nginx作為正向代理服務器支持http協(xié)議的配置。
代理服務器:192.168.110.101
- 代理服務器配置:
server { listen 8080; server_name localhost; # 解析域名時需要配置 resolver 8.8.8.8; location / { proxy_pass http://$host$request_uri; } }
- 客戶端配置:
我們使用Windows系統(tǒng)作為客戶端環(huán)境。
- 訪問
http://nginx.org/en/index.html
,可以正常訪問。
- 訪問
https://www.baidu.com
,則無法正常訪問了。
- 查看代理服務器的error.log,發(fā)現其報400錯誤碼。
這是因為,Nginx作為正向代理服務器時,默認僅支持http協(xié)議,是不支持https協(xié)議的。
2.Nginx正向代理(https)
那么怎么讓Nginx作為正向代理服務器的時候支持https協(xié)議呢?
我們可以使用第三方模塊ngx_http_proxy_connect_module
。
下載地址:https://github.com/chobits/ngx_http_proxy_connect_module
我們知道如果要為Nginx添加第三方模塊,需要在配置configure
時添加--add-module
。從Nginx1.9.11版本開始,支持load_module
指令來動態(tài)加載模塊。
我們這里使用--add-module
進行模塊的添加。
1)查看Nginx版本以及configure信息
nginx -V nginx version: nginx/1.22.1 built by gcc 9.4.0 (Ubuntu 9.4.0-1ubuntu1~20.04.1) configure arguments: --sbin-path=/usr/local/nginx/nginx --conf-path=/usr/local/nginx/nginx.conf --pid-path=/usr/local/nginx/nginx.pid --with-http_gzip_static_module
2)下載模塊
下載地址:https://codeload.github.com/chobits/ngx_http_proxy_connect_module/zip/refs/heads/master
3)重新編譯
這里我們兩種添加第三方模塊的方式都嘗試一下。
- 使用
--add-module
cd /home/stone/nginx-1.22.1 # 1、添加patch patch -p1 < /home/stone/nginx-1.22.1/module/ngx_http_proxy_connect_module/patch/proxy_connect_rewrite_102101.patch # 2、configure ./configure --sbin-path=/usr/local/nginx/nginx --conf-path=/usr/local/nginx/nginx.conf --pid-path=/usr/local/nginx/nginx.pid --with-http_gzip_static_module --add-module=/home/stone/nginx-1.22.1/module/ngx_http_proxy_connect_module # 3、make make # 4、備份舊的nginx可執(zhí)行文件,復制編譯之后的可執(zhí)行文件 mv /usr/local/nginx/nginx /usr/local/nginx/nginx.old cp objs/nginx /usr/local/nginx/nginx # 5、升級 make upgrade
- 使用
load_module
,需要將--add-module
替換為--add-dynamic-module
cd /home/stone/nginx-1.22.1 # 1、添加patch patch -p1 < /home/stone/nginx-1.22.1/module/ngx_http_proxy_connect_module/patch/proxy_connect_rewrite_102101.patch # 2、configure ./configure --sbin-path=/usr/local/nginx/nginx --conf-path=/usr/local/nginx/nginx.conf --pid-path=/usr/local/nginx/nginx.pid --with-http_gzip_static_module --add-dynamic-module=/home/stone/nginx-1.22.1/module/ngx_http_proxy_connect_module # 3、make make # 4 創(chuàng)建module文件夾,并將編譯生成的ngx_http_proxy_connect_module.so拷貝過去 mkdir /usr/local/nginx/module cp /home/stone/nginx-1.22.1/objs/ngx_http_proxy_connect_module.so /usr/local/nginx/module # 5、備份舊的nginx可執(zhí)行文件,復制編譯之后的可執(zhí)行文件 mv /usr/local/nginx/nginx /usr/local/nginx/nginx.old cp objs/nginx /usr/local/nginx/nginx # 6、升級 make upgrade
4)修改配置文件
# --add-dynamic-module動態(tài)添加第三方模塊時使用 # load_module module/ngx_http_proxy_connect_module.so; http { server { listen 8080; server_name localhost; resolver 114.114.114.114 ipv6=off; proxy_connect; proxy_connect_allow 443 80; proxy_connect_connect_timeout 10s; proxy_connect_data_timeout 10s; # 指定代理日志 access_log logs/access_proxy.log main; location / { proxy_pass $scheme://$host$request_uri; } } }
此時訪問https://www.baidu.com
,在access_proxy.log
產生如下日志,說明https代理成功。
3.Nginx反向代理(http)
同樣的,Nginx作為反向代理服務器,默認也是只支持http協(xié)議,我們來回顧一下Nginx作為反向代理服務器支持http協(xié)議的配置。
server { listen 80; server_name localhost; location /proxy { proxy_set_header X-Real-IP $remote_addr; proxy_pass http://192.168.110.98; } }
可以看到,我們配置的server_name
為localhost
,但在實際項目中,我們是使用域名綁定Nginx服務器的IP,并且使用https協(xié)議進行訪問,配置的server_name
就是指定的域名,例如www.aaa.com
。
Nginx為我們提供了ngx_http_ssl_module
來支持https協(xié)議,并且在提供的默認配置文件里已經給出了示例。
4.Nginx反向代理(https)
添加ngx_http_ssl_module
的步驟和添加ngx_http_proxy_connect_module
的步驟一致,只是這是Nginx提供的模塊,因此在configure時使用--with-http_ssl_module
即可。
我們再來看看采用https協(xié)議時的配置:
server { listen 443 ssl; server_name www.aaa.com; # 申請ssl證書后,會提供cert.pem和cert.key ssl_certificate cert.pem; ssl_certificate_key cert.key; ssl_session_cache shared:SSL:1m; ssl_session_timeout 5m; ssl_ciphers HIGH:!aNULL:!MD5; ssl_prefer_server_ciphers on; location /proxy { proxy_pass http://192.168.110.98; } }
我們需要去為指定的域名申請ssl證書,然后將證書中的cert.pem
和cert.key
放到指定文件,并在配置文件中指定。例如我們這里指定的server_name
為www.aaa.com
,所以我們就需要為www.aaa.com
申請ssl證書。
后續(xù)我們訪問https://www.aaa.com/proxy
就可以被代理到指定服務端了。
總結
以上就是Nginx實現正向代理和反向代理支持https協(xié)議的全部內容,Nginx是多模塊化的,還有很多高級功能,我們后面繼續(xù)探索。
這些僅為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。