Nginx接收Http協議請求轉發使用Https協議的問題
公司使用阿里的apigateway,規定不太友好,同是SIT環境,A系統的SIT1環境居然不能調用B系統的SIT2環境的接口。因為各個系統之間部署的SIT環境數量不同A系統可能只有1套,B系統可能有8套,這樣的話,可能會隨時切換調用B系統的環境,管理員不允許,于是想著用Nginx做下轉發。因為A系統調用B系統是內部調用,不計劃使用HTTPS,因為還要去申請證書,但是B系統調用入口必須使用HTTPS,這樣就要求Nginx可以接收HTTP協議的請求,轉發出去的協議是HTTPS。
第一次配置Nginxserver {listen 10000;server_name 192.168.1.2;error_page 500 502 503 504 /50x.html;location = /50x.html { root html;}location / { proxy_pass https://aaa.bbb.com:9000;} }以為這樣就可以直接轉發了,但是執行nginx -t直接報錯:
nginx: [emerg] https protocol requires SSL support in /data/nginx/conf/nginx.conf:224nginx: configuration file /data/nginx/conf/nginx.conf test failed
224行就是我上面的proxy_pass https://aaa.bbb.com:9000;這一行搜了一下說是nginx當時編譯的時候沒有http_ssl_module模塊,使用nginx -V查看一下當時編譯的參數:
nginx version: nginx/1.21.5built by gcc 4.8.5 (SUSE Linux) configure arguments: --prefix=/data/nginx --with-pcre=/data/software/pcre-8.21 --with-zlib=/data/software/zlib-1.2.11 --with-openssl=/etc/ssl
果然沒有http_ssl_module模塊,于是決定重新編譯一下nginx。
重新編譯Nginx注意:我的輸出是/data/nginx,和當前正在跑的Nginx是同一個目錄,先使用nginx -s stop停止nginx,然后備份conf/nginx.conf文件,防止被覆蓋。
先安裝依賴:pcre-8.21,zlib-1.2.11,openssl-1.0.2t我都是下載的源碼,然后編譯并安裝的
# pcre-8.21 使用以下命令cd pcre-8.21 && ./configure && make && make install# zlib-1.2.11 使用以下命令cd zlib-1.2.11 && ./configure && make && make install# openssl-1.0.2t 比較特殊 使用cd openssl-1.0.2t && ./config && make && make install進入Nginx源碼目錄然后使用以下命令configure:
./configure --prefix=/data/nginx --with-pcre=/data/software/pcre-8.21 --with-zlib=/data/software/zlib-1.2.11 --with-openssl=/data/software/openssl-1.0.2t --with-http_ssl_module然后執行編譯和安裝:
make && make install重啟Nginx編譯完成后發現之前的Nginx二進制文件變成了nginx.old,新的Nginx文件叫nginx,給這個nginx二進制加執行權限,然后執行
nginx -t此時不再報錯,提示成功:
nginx: the configuration file /data/nginx/conf/nginx.conf syntax is oknginx: configuration file /data/nginx/conf/nginx.conf test is successful
然后查看conf/nginx.conf,發現沒有被覆蓋,可以直接啟動Nginx了:
nginx -c /data/nginx/conf/nginx.conf啟動完成后用ps命令查看以下進程果然在。然后使用postman測試,發現可以正確轉發,大功告成。
到此這篇關于Nginx接收Http協議請求轉發使用Https協議的文章就介紹到這了,更多相關Nginx接收Http協議請求內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!
