Nginx配置正向代理和反向代理,實現(xiàn)HTTPS通信的案例

問題描述

問題描述

客戶端直接通過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
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容