最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Nginx內(nèi)網(wǎng)環(huán)境開啟https雙協(xié)議的實(shí)現(xiàn)

 更新時(shí)間:2025年02月21日 08:33:53   作者:Mr-Wanter  
本文主要介紹了Nginx內(nèi)網(wǎng)環(huán)境開啟https雙協(xié)議,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

前言

nginx開啟https前提:

  • 服務(wù)器支持open-ssl
  • nginx 包含--with-http_ssl_module --with-stream --with-stream_ssl_preread_module模塊

一、open-ssl

1. 驗(yàn)證

openssl version

2. 安裝

 mkdir /usr/local/ssl
 cd /usr/local/ssl
 # 解壓
 tar -xf openssl-3.0.1.tar.gz
 # 設(shè)置SSL庫文件路徑
 ./config --prefix=/usr/local/ssl/
 make
 make install
vi /etc/ld.so.conf
# 最后一行添加/usr/local/ssl/ 路徑
sudo ldconfig 

常見報(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)建私鑰的過程中,由于必須要指定一個(gè)密碼。而這個(gè)密碼會(huì)帶來一個(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è)開啟http的端口
        server 127.0.0.1:8991;
    }
    upstream https_protocol {
        # 10002端口是一個(gè)開啟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è)問題是很大的難點(diǎn),排查好久才找到一個(gè)對(duì)應(yīng)版本安裝成功(我的是麒麟銀河V10,版本OpenSSL 1.1.1f),關(guān)鍵是怎么找到對(duì)應(yīng)版本的過程當(dāng)時(shí)沒有記錄,現(xiàn)在也想不起來了,??
  • open-ssl驗(yàn)證時(shí)本地發(fā)現(xiàn)有open-ssl,所以就跳過了第一步 結(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的配置過程中,你可能沒有正確指定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é)議不支持獲取訪問ip穿透
    更改為https或者h(yuǎn)ttp單協(xié)議可獲取到客戶端訪問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)境開啟https雙協(xié)議的文章就介紹到這了,更多相關(guān)Nginx開啟https雙協(xié)議內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Nginx中break與last的區(qū)別詳析

    Nginx中break與last的區(qū)別詳析

    這篇文章主要給大家介紹了關(guān)于Nginx中break與last區(qū)別的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-01-01
  • Nginx實(shí)現(xiàn)會(huì)話保持的兩種方式

    Nginx實(shí)現(xiàn)會(huì)話保持的兩種方式

    在我們做Nginx負(fù)載均衡的時(shí)候經(jīng)常會(huì)遇到會(huì)話保持的問題,為了保證同一用戶session會(huì)被分配到同一臺(tái)服務(wù)器上,這時(shí)就需要會(huì)話保持,本文介紹了兩種方法,感興趣的可以了解一下
    2022-03-03
  • Nginx設(shè)置連接超時(shí)并進(jìn)行測(cè)試的方法步驟

    Nginx設(shè)置連接超時(shí)并進(jìn)行測(cè)試的方法步驟

    在高并發(fā)場(chǎng)景下,如果客戶端與服務(wù)器的連接長時(shí)間未響應(yīng),會(huì)占用大量的系統(tǒng)資源,影響其他正常請(qǐng)求的處理效率,為了解決這個(gè)問題,可以通過設(shè)置?Nginx?的連接超時(shí)時(shí)間來優(yōu)化資源管理,提高服務(wù)器的穩(wěn)定性,以下是具體的配置方法和測(cè)試步驟,希望對(duì)你有所幫助
    2025-02-02
  • Nginx暴露出請(qǐng)求的真實(shí)IP的問題

    Nginx暴露出請(qǐng)求的真實(shí)IP的問題

    在工作中,經(jīng)常會(huì)用用戶實(shí)際請(qǐng)求的IP地址,本文主要介紹了Nginx暴露出請(qǐng)求的真實(shí)IP的問題,具有一定的參考價(jià)值,感興趣的可以了解一下
    2023-10-10
  • 如何用nginx配置wordpress的方法示例

    如何用nginx配置wordpress的方法示例

    這篇文章主要介紹了如何用nginx配置wordpress的方法示例,詳細(xì)的介紹了配置步驟,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-09-09
  • nginx安裝完成無法解析php解決方法

    nginx安裝完成無法解析php解決方法

    大家好,本篇文章主要講的是nginx安裝完成無法解析php解決方法,感興趣的同學(xué)趕快來看一看吧,對(duì)你有幫助的話記得收藏一下,方便下次瀏覽
    2021-12-12
  • Nginx出現(xiàn)403 Forbidden問題的常見原因與解決

    Nginx出現(xiàn)403 Forbidden問題的常見原因與解決

    Nginx 返回 403 Forbidden 錯(cuò)誤通常表示客戶端沒有權(quán)限訪問請(qǐng)求的資源,這種錯(cuò)誤有許多可能的原因,本文將為大家介紹一下常見的原因和對(duì)應(yīng)的解決方法,希望對(duì)大家有所幫助
    2025-03-03
  • nginx配置長連接、短連接、WebSocket的實(shí)現(xiàn)步驟

    nginx配置長連接、短連接、WebSocket的實(shí)現(xiàn)步驟

    在Nginx中,您可以通過配置來控制長連接、短連接以及?WebSocket?的使用,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2026-02-02
  • 詳解nginx配置location總結(jié)及rewrite規(guī)則寫法

    詳解nginx配置location總結(jié)及rewrite規(guī)則寫法

    本篇文章主要介紹了詳解nginx配置location總結(jié)及rewrite規(guī)則寫法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-02-02
  • nginx快速部署一個(gè)網(wǎng)站服務(wù)(多域名+多端口)

    nginx快速部署一個(gè)網(wǎng)站服務(wù)(多域名+多端口)

    本文主要介紹了nginx快速部署一個(gè)網(wǎng)站服務(wù),并實(shí)現(xiàn)多域名和多端口,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-10-10

最新評(píng)論

汾西县| 晋宁县| 化隆| 隆回县| 鄂伦春自治旗| 义乌市| 台中市| 永城市| 织金县| 罗甸县| 三都| 茂名市| 南华县| 新化县| 临猗县| 历史| 汉寿县| 丽水市| 库伦旗| 平定县| 哈巴河县| 丹江口市| 武城县| 教育| 滨海县| 宽甸| 婺源县| 中西区| 包头市| 友谊县| 云安县| 专栏| 新闻| 池州市| 灵璧县| 宜都市| 疏勒县| 鲁甸县| 沅陵县| 靖安县| 什邡市|