問題描述

問題描述
客戶端直接通過HTTP訪問內(nèi)部服務(wù)是不安全的,如果在不改造客戶端和內(nèi)部服務(wù)的情況下實現(xiàn)HTTPS的安全信息傳輸?這里使用了nginx的正向代理和反向代理,如上圖所示。
使用OpenSSL生成證書
openssl req -x509 -nodes -days 36500 -newkey rsa:2048 -keyout /home/data/ssl/nginx.key -out /home/data/ssl/nginx.crt

image.png
其中proxy.test.com為綁定的域名。
Nginx反向代理配置
server {
listen 443 ssl;
server_name proxy.test.com;
root /home/data/proxy;
ssl_certificate /home/data/ssl/nginx.crt;
ssl_certificate_key /home/data/ssl/nginx.key;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
#禁止在header中出現(xiàn)服務(wù)器版本,防止黑客利用版本漏洞攻擊
server_tokens off;
#如果是全站 HTTPS 并且不考慮 HTTP 的話,可以加入 HSTS 告訴你的瀏覽器本網(wǎng)站全站加密,并且強制用 HTTPS 訪問
fastcgi_param HTTPS on;
fastcgi_param HTTP_SCHEME https;
location /api/
{
rewrite ^/api/(.*) /$1 break;
proxy_pass http://192.168.10.191:8080;
}
}
Nginx正向代理的配置
server {
listen 80;
server_name www.forwordproxy.com;
resolver 8.8.8.8;
location / {
proxy_pass https://$http_host$request_uri;
}
error_page 500 502 503 504 /index.html;
location = /index.html {
root /home/data/forwordproxy;
}
}
使用wget進行測試
驗證證書
wget --ca-certificate=/home/data/ssl/nginx.crt -e "http_proxy=http://www.forwordproxy.com" "https://proxy.test.com/interface/test"
使用上面生成的nging.crt證書,通過正向代理,訪問反向代理服務(wù)接口。
不驗證證書
wget --no-check-certificate -e "http_proxy=http://www.forwordproxy.com" "https://proxy.test.com/interface/test"
備注
- 域名都需要配置/etc/hosts