Nginx內(nèi)網(wǎng)環(huán)境開(kāi)啟https雙協(xié)議的實(shí)現(xiàn)
前言
nginx開(kāi)啟https前提:
- 服務(wù)器支持open-ssl
- nginx 包含
--with-http_ssl_module --with-stream --with-stream_ssl_preread_module
模塊
一、open-ssl
1. 驗(yàn)證
openssl version
2. 安裝
- 下載openssl安裝包openssl安裝包
- 安裝openssl
mkdir /usr/local/ssl cd /usr/local/ssl # 解壓 tar -xf openssl-3.0.1.tar.gz # 設(shè)置SSL庫(kù)文件路徑 ./config --prefix=/usr/local/ssl/ make make install
vi /etc/ld.so.conf # 最后一行添加/usr/local/ssl/ 路徑 sudo ldconfig
常見(jiàn)報(bào)錯(cuò):openssl: error while loading shared libraries: libssl.so.10: cannot open shared object file: No such file or directory
系統(tǒng)版本和openssl版本不一致,具體哪里的日志記錄需要的版本忘記了
3.生成ssl證書
# 第一步:生成私鑰 mkdir /etc/ssl/certs/www.abc.com cd /etc/ssl/certs/www.abc.com openssl genrsa -des3 -out server.key 2048 # 輸入一個(gè)4位以上的密碼 # 確認(rèn)密碼 #第二步:生成CSR(證書簽名請(qǐng)求) openssl req -new -key server.key -out server.csr -subj "/C=CN/ST=JiLin/L=ChangChun/O=commany/OU=commany/CN=www.abc.com" #第三步:去除私鑰中的密碼 #在第1步創(chuàng)建私鑰的過(guò)程中,由于必須要指定一個(gè)密碼。而這個(gè)密碼會(huì)帶來(lái)一個(gè)副作用,那就是在每次啟動(dòng)Web服務(wù)器時(shí),都會(huì)要求輸入密碼 #這顯然非常不方便。要?jiǎng)h除私鑰中的密碼,操作如下: openssl rsa -in server.key -out server.key #第四步:生成自簽名SSL證書 # -days 證書有效期-天 openssl x509 -req -days 3650 -in server.csr -signkey server.key -out server.crt
一、nginx
1. 驗(yàn)證支持模塊
nginx -V
2. 安裝必要模塊
可以參考我之前的博客 Nginx 平滑升級(jí)
2.1 重新編譯nginx
./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-pcre --with-http_gzip_static_module --with-stream --with-stream_ssl_preread_module
生成nginx二進(jìn)制執(zhí)行文件到當(dāng)前目錄 /objs
make
2.2 替換原文件
替換
mv /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak cp /usr/local/nginx-1.13.3/objs/nginx /usr/local/nginx/sbin/
驗(yàn)證
[root@web nginx-1.21.5]# make upgrade /usr/local/nginx/sbin/nginx -t nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful kill -USR2 `cat /usr/local/nginx/logs/nginx.pid` sleep 1 test -f /usr/local/nginx/logs/nginx.pid.oldbin kill -QUIT `cat /usr/local/nginx/logs/nginx.pid.oldbin`
升級(jí)
#驗(yàn)證模塊是否加載成功 nginx -V
3. 配置https
下面是一段雙協(xié)議支持的配置代碼
請(qǐng)?jiān)试S我抄襲一下小左同學(xué)的代碼
stream { upstream http_protocol { # 8991端口是一個(gè)開(kāi)啟http的端口 server 127.0.0.1:8991; } upstream https_protocol { # 10002端口是一個(gè)開(kāi)啟https的端口 server 127.0.0.1:10002; } # 根據(jù)不同的協(xié)議走不同的upstream map $ssl_preread_protocol $upstream { default http_protocol; "TLSv1.0" https_protocol; "TLSv1.1" https_protocol; "TLSv1.2" https_protocol; "TLSv1.3" https_protocol; } server { listen 8990; ssl_preread on; proxy_pass $upstream; } }
server { listen 10002 ssl; server_name www.xxx.com; ssl_certificate /etc/ssl/certs/www.abc.com/server.crt; ssl_certificate_key /etc/ssl/certs/www.abc.com/server.key; #減少點(diǎn)擊劫持 #add_header X-Frame-Options DENY; add_header X-Frame-Options AllowAll; #禁止服務(wù)器自動(dòng)解析資源類型 add_header X-Content-Type-Options nosniff; #防XSS攻擊 add_header X-Xss-Protection 1; #優(yōu)先采取服務(wù)器算法 ssl_prefer_server_ciphers on; #協(xié)議 ssl_protocols TLSv1 TLSv1.1 TLSv1.2; ssl_session_cache shared:SSL:10m; ssl_session_timeout 10m; location / { proxy_pass http://127.0.0.1:8991/; } }
總結(jié)
openssl: error while loading shared libraries: libssl.so.10: cannot open shared object file: No such file or directory
這個(gè)問(wèn)題是很大的難點(diǎn),排查好久才找到一個(gè)對(duì)應(yīng)版本安裝成功(我的是麒麟銀河V10,版本OpenSSL 1.1.1f),關(guān)鍵是怎么找到對(duì)應(yīng)版本的過(guò)程當(dāng)時(shí)沒(méi)有記錄,現(xiàn)在也想不起來(lái)了,??- open-ssl驗(yàn)證時(shí)本地發(fā)現(xiàn)有open-ssl,所以就跳過(guò)了第一步 結(jié)果nginx make報(bào)錯(cuò)
make -f objs/Makefile make[1]: Entering directory '/opt/nginx-1.21.5' cd /usr/local/ssl/ \ && if [ -f Makefile ]; then make clean; fi \ && ./config --prefix=/usr/local/ssl//.openssl no-shared no-threads \ && make \ && make install_sw LIBDIR=lib /bin/sh: line 2: ./config: No such file or directory make[1]: *** [objs/Makefile:1447: /usr/local/ssl//.openssl/include/openssl/ssl.h] Error 127
OpenSSL源代碼未正確指定:在Nginx的配置過(guò)程中,你可能沒(méi)有正確指定OpenSSL的源代碼目錄。你需要確保–with-openssl選項(xiàng)指向的是OpenSSL的源代碼目錄,而不是安裝目錄。
上傳openssl-1.1.1f.tar.gz包(和驗(yàn)證時(shí)的版本一致即可)解壓后指定–with-openssl到解壓目錄--with-openssl=/opt/openssl-1.1.1f
./configure \ --prefix=/usr/local/nginx \ --user=nginx \ --group=nginx \ --with-pcre \ --with-openssl=/opt/openssl-1.1.1f \ --with-http_ssl_module \ --with-http_v2_module \ --with-http_realip_module \ --with-http_addition_module \ --with-http_sub_module \ --with-http_dav_module \ --with-http_flv_module \ --with-http_mp4_module \ --with-http_gunzip_module \ --with-http_gzip_static_module \ --with-http_random_index_module \ --with-http_secure_link_module \ --with-http_stub_status_module \ --with-http_auth_request_module \ --with-http_image_filter_module \ --with-mail \ --with-threads \ --with-mail_ssl_module \ --with-stream_ssl_module \ --with-stream --with-stream_ssl_preread_module \ && make
- 雙協(xié)議不支持獲取訪問(wèn)ip穿透
更改為https或者h(yuǎn)ttp單協(xié)議可獲取到客戶端訪問(wèn)ip,如果代理中包含websocket需要把響應(yīng)代理放到和ssl配置的配置文件中
關(guān)鍵配置:
proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
nginx.conf 示例
user root; worker_processes auto; error_log /usr/local/nginx/logs/error.log; events { worker_connections 1024; } http { # log_format main '$remote_addr - $remote_user [$time_local] "$request" ' # '$status $body_bytes_sent "$http_referer" ' # '"$http_user_agent" "$http_x_forwarded_for"'; log_format main '$year$month$day $hour:$minutes:$seconds ' '[$status] ' '【$http_x_forwarded_for $remote_addr $http_host】' '[$request_uri] ' ; access_log /usr/local/nginx/logs/access.log main; underscores_in_headers on; sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 65; types_hash_max_size 4096; include /etc/nginx/mime.types; client_max_body_size 10m; default_type application/octet-stream; #default_type text/html; #gzip gzip on; gzip_min_length 1024; gzip_comp_level 6; gzip_types text/plain application/json application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png application/vnd.ms-fontobject font/ttf font/opentype font/x-woff image/svg+xml font/woff; gzip_vary on; gzip_disable "MSIE [1-6]\."; gzip_buffers 32 16k; gzip_http_version 1.0; include /usr/local/nginx/conf/conf.d/*.conf; server { listen 8990 ssl; server_name www.bbcc.com; ssl_certificate /etc/ssl/certs/www.bbcc.com/server.crt; ssl_certificate_key /etc/ssl/certs/www.bbcc.com/server.key; #減少點(diǎn)擊劫持 #add_header X-Frame-Options DENY; add_header X-Frame-Options AllowAll; #禁止服務(wù)器自動(dòng)解析資源類型 add_header X-Content-Type-Options nosniff; #防XSS攻擊 add_header X-Xss-Protection 1; #優(yōu)先采取服務(wù)器算法 ssl_prefer_server_ciphers on; #協(xié)議 ssl_protocols TLSv1 TLSv1.1 TLSv1.2; ssl_session_cache shared:SSL:10m; ssl_session_timeout 10m; # 自定義時(shí)間變量 if ($time_iso8601 ~ "^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})") { set $year $1; set $month $2; set $day $3; set $hour $4; set $minutes $5; set $seconds $6; } location / { autoindex off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass http://172.168.18.31:8990/; } location /gws { proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; proxy_pass http://172.168.18.31:8990; } } }
到此這篇關(guān)于Nginx內(nèi)網(wǎng)環(huán)境開(kāi)啟https雙協(xié)議的文章就介紹到這了,更多相關(guān)Nginx開(kāi)啟https雙協(xié)議內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Linux下nginx配置https協(xié)議訪問(wèn)的方法
- Nexus使用nginx代理實(shí)現(xiàn)支持HTTPS協(xié)議
- Nginx接收Http協(xié)議請(qǐng)求轉(zhuǎn)發(fā)使用Https協(xié)議的問(wèn)題
- nginx將https協(xié)議反向代理到http協(xié)議請(qǐng)求上的實(shí)現(xiàn)
- 如何實(shí)現(xiàn)Nginx同一端口同時(shí)支持http與https協(xié)議
- nginx將https協(xié)議反向代理到http協(xié)議請(qǐng)求上
- Nginx服務(wù)器配置https安全協(xié)議的實(shí)現(xiàn)
相關(guān)文章
nginx中一個(gè)請(qǐng)求的count計(jì)數(shù)跟蹤淺析
這篇文章主要給大家介紹了關(guān)于nginx中一個(gè)請(qǐng)求的count計(jì)數(shù)跟蹤的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2022-01-01關(guān)于nginx+php5.3.8+eclipse3.7工作空間的配置方法
以前用eclipse3.6時(shí)設(shè)置php服務(wù)器時(shí)完全可以在base url欄填寫自己工作空間的目錄,然后修改nginx.conf加一個(gè)alias就行了2011-11-11nginx代理后端路徑獲取IP為127.0.0.1問(wèn)題
文章討論了在使用Nginx作為反向代理時(shí),如何正確配置以避免在前端路徑A/api訪問(wèn)后端時(shí)丟失真實(shí)的IP地址,通過(guò)有效的Nginx配置,可以確保在前后端分離的場(chǎng)景中,客戶端通過(guò)前端路徑訪問(wèn)后端時(shí),后端能夠正確獲取客戶端的真實(shí)IP地址,示例配置展示了如何實(shí)現(xiàn)這一目標(biāo)2025-02-02Nginx出現(xiàn)404 Not Found nginx/1.23.4的完美解決方案
在Nginx配置過(guò)程中,404 Not Found錯(cuò)誤是一個(gè)常見(jiàn)問(wèn)題,本文將詳細(xì)解析Nginx 404 Not Found的原因及解決方案,確保您能夠輕松解決這一問(wèn)題,需要的小伙伴跟著小編一起來(lái)學(xué)習(xí)學(xué)習(xí)吧2024-07-07nginx設(shè)置目錄白名單、ip白名單的實(shí)現(xiàn)方法
今天小編就為大家分享一篇nginx設(shè)置目錄白名單、ip白名單的實(shí)現(xiàn)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-08-08nginx訪問(wèn)控制的實(shí)現(xiàn)示例
這篇文章主要介紹了nginx訪問(wèn)控制的實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11