Nginx內(nèi)網(wǎng)環(huán)境開啟https雙協(xié)議的實現(xiàn)
前言
nginx開啟https前提:
- 服務器支持open-ssl
- nginx 包含
--with-http_ssl_module --with-stream --with-stream_ssl_preread_module
模塊
一、open-ssl
1. 驗證
openssl version
2. 安裝
- 下載openssl安裝包openssl安裝包
- 安裝openssl
mkdir /usr/local/ssl cd /usr/local/ssl # 解壓 tar -xf openssl-3.0.1.tar.gz # 設置SSL庫文件路徑 ./config --prefix=/usr/local/ssl/ make make install
vi /etc/ld.so.conf # 最后一行添加/usr/local/ssl/ 路徑 sudo ldconfig
常見報錯: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 # 輸入一個4位以上的密碼 # 確認密碼 #第二步:生成CSR(證書簽名請求) 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)建私鑰的過程中,由于必須要指定一個密碼。而這個密碼會帶來一個副作用,那就是在每次啟動Web服務器時,都會要求輸入密碼 #這顯然非常不方便。要刪除私鑰中的密碼,操作如下: 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. 驗證支持模塊
nginx -V
2. 安裝必要模塊
可以參考我之前的博客 Nginx 平滑升級
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二進制執(zhí)行文件到當前目錄 /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/
驗證
[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`
升級
#驗證模塊是否加載成功 nginx -V
3. 配置https
下面是一段雙協(xié)議支持的配置代碼
請允許我抄襲一下小左同學的代碼
stream { upstream http_protocol { # 8991端口是一個開啟http的端口 server 127.0.0.1:8991; } upstream https_protocol { # 10002端口是一個開啟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; #減少點擊劫持 #add_header X-Frame-Options DENY; add_header X-Frame-Options AllowAll; #禁止服務器自動解析資源類型 add_header X-Content-Type-Options nosniff; #防XSS攻擊 add_header X-Xss-Protection 1; #優(yōu)先采取服務器算法 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/; } }
總結
openssl: error while loading shared libraries: libssl.so.10: cannot open shared object file: No such file or directory
這個問題是很大的難點,排查好久才找到一個對應版本安裝成功(我的是麒麟銀河V10,版本OpenSSL 1.1.1f),關鍵是怎么找到對應版本的過程當時沒有記錄,現(xiàn)在也想不起來了,??- open-ssl驗證時本地發(fā)現(xiàn)有open-ssl,所以就跳過了第一步 結果nginx make報錯
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的配置過程中,你可能沒有正確指定OpenSSL的源代碼目錄。你需要確保–with-openssl選項指向的是OpenSSL的源代碼目錄,而不是安裝目錄。
上傳openssl-1.1.1f.tar.gz包(和驗證時的版本一致即可)解壓后指定–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é)議不支持獲取訪問ip穿透
更改為https或者http單協(xié)議可獲取到客戶端訪問ip,如果代理中包含websocket需要把響應代理放到和ssl配置的配置文件中
關鍵配置:
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; #減少點擊劫持 #add_header X-Frame-Options DENY; add_header X-Frame-Options AllowAll; #禁止服務器自動解析資源類型 add_header X-Content-Type-Options nosniff; #防XSS攻擊 add_header X-Xss-Protection 1; #優(yōu)先采取服務器算法 ssl_prefer_server_ciphers on; #協(xié)議 ssl_protocols TLSv1 TLSv1.1 TLSv1.2; ssl_session_cache shared:SSL:10m; ssl_session_timeout 10m; # 自定義時間變量 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; } } }
到此這篇關于Nginx內(nèi)網(wǎng)環(huán)境開啟https雙協(xié)議的文章就介紹到這了,更多相關Nginx開啟https雙協(xié)議內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
關于nginx+php5.3.8+eclipse3.7工作空間的配置方法
以前用eclipse3.6時設置php服務器時完全可以在base url欄填寫自己工作空間的目錄,然后修改nginx.conf加一個alias就行了2011-11-11Nginx出現(xiàn)404 Not Found nginx/1.23.4的完美解決方案
在Nginx配置過程中,404 Not Found錯誤是一個常見問題,本文將詳細解析Nginx 404 Not Found的原因及解決方案,確保您能夠輕松解決這一問題,需要的小伙伴跟著小編一起來學習學習吧2024-07-07