欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

nginx正向代理https網(wǎng)站的實(shí)現(xiàn)

 更新時(shí)間:2024年05月31日 09:15:13   作者:碼農(nóng)心語(yǔ)  
Nginx正向代理,通過(guò)服務(wù)器代理客戶(hù)端去重定向請(qǐng)求訪問(wèn)到目標(biāo)服務(wù)器的一種代理服務(wù),本文主要介紹了nginx正向代理https網(wǎng)站的實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下

1. 緣起

最近碰到了一個(gè)麻煩事情,就是公司的centos測(cè)試服務(wù)器放在內(nèi)網(wǎng)環(huán)境,而且不能直接上外網(wǎng),導(dǎo)致無(wú)法通過(guò)yum安裝軟件,非常捉急。

幸好,內(nèi)網(wǎng)還是有可以可以訪問(wèn)外網(wǎng)的機(jī)器,所以就想到應(yīng)該可以利用nginx搭建一個(gè)代理服務(wù)器,然后centos通過(guò)這個(gè)nginx來(lái)訪問(wèn)外網(wǎng)。當(dāng)然,如果只是代理http還是很簡(jiǎn)單的,而要代理https還是需要稍費(fèi)周折,因?yàn)閚ginx本身不能部署被代理的網(wǎng)站的證書(shū),不能部署成https終結(jié)點(diǎn)來(lái),因此與被代理客戶(hù)端之間不能用ssl協(xié)議通訊,因此需要通過(guò)http協(xié)議中的CONNECT請(qǐng)求打通和外網(wǎng)的連接,然后客戶(hù)端到nginx走明文,nginx到外網(wǎng)走h(yuǎn)ttps協(xié)議。這里需要用到ngx_http_proxy_connect_module模塊來(lái)實(shí)現(xiàn)CONNECT的代理功能。

2. 部署nginx

步驟1:從nginx官網(wǎng)下載nginx源碼包

步驟2:因?yàn)閚ginx原生是不支持CONNECT請(qǐng)求的,需要安裝一個(gè)擴(kuò)展插件,即ngx_http_proxy_connect_module,從github下載ngx_http_proxy_connect_module,另外還要下載一個(gè)nginx內(nèi)核補(bǔ)丁

步驟3: 解壓nginx源碼包,進(jìn)入nginx源碼目錄,創(chuàng)建modules目錄(mkdir modules)。

步驟4: 將ngx_http_proxy_connect_module源碼目錄放到modules目錄中。

步驟5: 將nginx內(nèi)核補(bǔ)丁放到nginx源碼目錄,姑且名字叫p1.patch

步驟6: 在nginx源碼目錄,執(zhí)行以下命令給nginx內(nèi)核打上補(bǔ)丁:

patch -p 1 < p1.patch

步驟7:編譯nginx,這里假設(shè)nginx安裝到/opt/nginx目錄中(在編譯前確認(rèn)pcre、zlib、openssl的庫(kù)是否已經(jīng)正常安裝),編譯命令如下:

./configure --prefix=/opt/nginx --with-http_ssl_module -add-module=./modules/ngx_http_proxy_connect_module
make & make install

步驟8:配置nginx

配置文件如下:

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    keepalive_timeout  65;


  server {
        # 代理端口
		listen 8080;
        server_name  localhost;
        
        # 解析被代理網(wǎng)站域名的dns服務(wù)器,根據(jù)實(shí)際情況自行配置
        resolver  114.114.114.114;
        
        # 開(kāi)啟proxy connect功能
        proxy_connect;
        
        # 設(shè)置允許代理的目標(biāo)端口為443,即https的默認(rèn)端口
        proxy_connect_allow 443 80;

        location / { 
        
            # 正向代理配置,根據(jù)請(qǐng)求地址自動(dòng)解析出目標(biāo)網(wǎng)站地址并進(jìn)行代理
            proxy_pass $scheme://$host$request_uri;
            
            # 發(fā)送到被代理網(wǎng)站的請(qǐng)求需要添加host頭
            proxy_set_header Host $http_host;
        
			proxy_buffers 256 4k; 
            proxy_max_temp_file_size 0;
            proxy_connect_timeout 30; 
        }
    }
}

以上配置完成后,通過(guò)nginx的8080端口,既可以代理普通http的請(qǐng)求,也可以代理https的請(qǐng)求。

步驟9:?jiǎn)?dòng)nginx

執(zhí)行/opt/nginx/sbin/nginx,啟動(dòng)nginx

3. 測(cè)試

3.1 http測(cè)試

curl "http://www.baidu.com/" -x 127.0.0.1:8080 -v

響應(yīng)內(nèi)容:

*   Trying 127.0.0.1:8080...
* Connected to (nil) (127.0.0.1) port 8080 (#0)
> GET http://www.baidu.com/ HTTP/1.1
> Host: www.baidu.com
> User-Agent: curl/7.81.0
> Accept: */*
> Proxy-Connection: Keep-Alive
> 
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< Server: nginx/1.24.0
< Date: Fri, 23 Feb 2024 09:08:01 GMT
< Content-Type: text/html
< Content-Length: 2381
< Connection: keep-alive
< Accept-Ranges: bytes
< Cache-Control: private, no-cache, no-store, proxy-revalidate, no-transform
< Etag: "588604eb-94d"
< Last-Modified: Mon, 23 Jan 2017 13:28:11 GMT
< Pragma: no-cache
< Set-Cookie: BDORZ=27315; max-age=86400; domain=.baidu.com; path=/
< 
<!DOCTYPE html>
<!--STATUS OK--><html> <head><meta http-equiv=content-type content=text/html;charset=utf-8><meta http-equiv=X-UA-Compatible content=IE=Edge><meta content=always name=referrer><link rel=stylesheet type=text/css href=http://s1.bdstatic.com/r/www/cache/bdorz/baidu.min.css><title>百度一下,你就知道</title></head> <body link=#0000cc> <div id=wrapper> <div id=head> <div class=head_wrapper> <div class=s_form> <div class=s_form_wrapper> <div id=lg> <img hidefocus=true src=//www.baidu.com/img/bd_logo1.png width=270 height=129> </div> <form id=form name=f action=//www.baidu.com/s class=fm> <input type=hidden name=bdorz_come value=1> <input type=hidden name=ie value=utf-8> <input type=hidden name=f value=8> <input type=hidden name=rsv_bp value=1> <input type=hidden name=rsv_idx value=1> <input type=hidden name=tn value=baidu><span class="bg s_ipt_wr"><input id=kw name=wd class=s_ipt value maxlength=255 autocomplete=off autofocus></span><span class="bg s_btn_wr"><input type=submit id=su value=百度一下 class="bg s_btn"></span> </form> </div> </div> <div id=u1> <a href=http://news.baidu.com name=tj_trnews class=mnav>新聞</a> <a href=http://www.hao123.com name=tj_trhao123 class=mnav>hao123</a> <a href=http://map.baidu.com name=tj_trmap class=mnav>地圖</a> <a href=http://v.baidu.com name=tj_trvideo class=mnav>視頻</a> <a href=http://tieba.baidu.com name=tj_trtieba class=mnav>貼吧</a> <noscript> <a href=http://www.baidu.com/bdorz/login.gif?login&amp;tpl=mn&amp;u=http%3A%2F%2Fwww.baidu.com%2f%3fbdorz_come%3d1 name=tj_login class=lb>登錄</a> </noscript> <script>document.write('<a + encodeURIComponent(window.location.href+ (window.location.search === " rel="external nofollow"  rel="external nofollow" " ? "?" : "&")+ "bdorz_come=1")+ '" name="tj_login" class="lb">登錄</a>');</script> <a href=//www.baidu.com/more/ name=tj_briicon class=bri style="display: block;">更多產(chǎn)品</a> </div> </div> </div> <div id=ftCon> <div id=ftConw> <p id=lh> <a href=http://home.baidu.com>關(guān)于百度</a> <a href=http://ir.baidu.com>About Baidu</a> </p> <p id=cp>&copy;2017 Baidu <a href=http://www.baidu.com/duty/>使用百度前必讀</a>  <a href=http://jianyi.baidu.com/ class=cp-feedback>意見(jiàn)反饋</a> 京ICP證030173號(hào)  <img src=//www.baidu.com/img/gs.gif> </p> </div> </div> </div> </body> </html>

通過(guò)以上的輸出可以看到http代理是沒(méi)有通過(guò)CONNECT請(qǐng)求進(jìn)行連接的,響應(yīng)正常。

3.2 https測(cè)試

 curl "https://www.baidu.com/" -x 127.0.0.1:8080 -v
*   Trying 127.0.0.1:8080...
* Connected to (nil) (127.0.0.1) port 8080 (#0)
* allocate connect buffer!
* Establish HTTP proxy tunnel to www.baidu.com:443
> CONNECT www.baidu.com:443 HTTP/1.1
> Host: www.baidu.com:443
> User-Agent: curl/7.81.0
> Proxy-Connection: Keep-Alive
> 
< HTTP/1.1 200 Connection Established
< Proxy-agent: nginx
< 
* Proxy replied 200 to CONNECT request
* CONNECT phase completed!
* ALPN, offering h2
* ALPN, offering http/1.1
*  CAfile: /etc/ssl/certs/ca-certificates.crt
*  CApath: /etc/ssl/certs
* TLSv1.0 (OUT), TLS header, Certificate Status (22):
* TLSv1.3 (OUT), TLS handshake, Client hello (1):
* TLSv1.2 (IN), TLS header, Certificate Status (22):
* TLSv1.3 (IN), TLS handshake, Server hello (2):
* TLSv1.2 (IN), TLS header, Certificate Status (22):
* TLSv1.2 (IN), TLS handshake, Certificate (11):
* TLSv1.2 (IN), TLS header, Certificate Status (22):
* TLSv1.2 (IN), TLS handshake, Server key exchange (12):
* TLSv1.2 (IN), TLS header, Certificate Status (22):
* TLSv1.2 (IN), TLS handshake, Server finished (14):
* TLSv1.2 (OUT), TLS header, Certificate Status (22):
* TLSv1.2 (OUT), TLS handshake, Client key exchange (16):
* TLSv1.2 (OUT), TLS header, Finished (20):
* TLSv1.2 (OUT), TLS change cipher, Change cipher spec (1):
* TLSv1.2 (OUT), TLS header, Certificate Status (22):
* TLSv1.2 (OUT), TLS handshake, Finished (20):
* TLSv1.2 (IN), TLS header, Finished (20):
* TLSv1.2 (IN), TLS header, Certificate Status (22):
* TLSv1.2 (IN), TLS handshake, Finished (20):
* SSL connection using TLSv1.2 / ECDHE-RSA-AES128-GCM-SHA256
* ALPN, server accepted to use http/1.1
* Server certificate:
*  subject: C=CN; ST=beijing; L=beijing; O=Beijing Baidu Netcom Science Technology Co., Ltd; CN=baidu.com
*  start date: Jul  6 01:51:06 2023 GMT
*  expire date: Aug  6 01:51:05 2024 GMT
*  subjectAltName: host "www.baidu.com" matched cert's "*.baidu.com"
*  issuer: C=BE; O=GlobalSign nv-sa; CN=GlobalSign RSA OV SSL CA 2018
*  SSL certificate verify ok.
* TLSv1.2 (OUT), TLS header, Supplemental data (23):
> GET / HTTP/1.1
> Host: www.baidu.com
> User-Agent: curl/7.81.0
> Accept: */*
> 
* TLSv1.2 (IN), TLS header, Supplemental data (23):
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< Accept-Ranges: bytes
< Cache-Control: private, no-cache, no-store, proxy-revalidate, no-transform
< Connection: keep-alive
< Content-Length: 2443
< Content-Type: text/html
< Date: Fri, 23 Feb 2024 09:11:25 GMT
< Etag: "58860410-98b"
< Last-Modified: Mon, 23 Jan 2017 13:24:32 GMT
< Pragma: no-cache
< Server: bfe/1.0.8.18
< Set-Cookie: BDORZ=27315; max-age=86400; domain=.baidu.com; path=/
< 
<!DOCTYPE html>
* TLSv1.2 (IN), TLS header, Supplemental data (23):
* TLSv1.2 (IN), TLS header, Supplemental data (23):
<!--STATUS OK--><html> <head><meta http-equiv=content-type content=text/html;charset=utf-8><meta http-equiv=X-UA-Compatible content=IE=Edge><meta content=always name=referrer><link rel=stylesheet type=text/css href=https://ss1.bdstatic.com/5eN1bjq8AAUYm2zgoY3K/r/www/cache/bdorz/baidu.min.css><title>百度一下,你就知道</title></head> <body link=#0000cc> <div id=wrapper> <div id=head> <div class=head_wrapper> <div class=s_form> <div class=s_form_wrapper> <div id=lg> <img hidefocus=true src=//www.baidu.com/img/bd_logo1.png width=270 height=129> </div> <form id=form name=f action=//www.baidu.com/s class=fm> <input type=hidden name=bdorz_come value=1> <input type=hidden name=ie value=utf-8> <input type=hidden name=f value=8> <input type=hidden name=rsv_bp value=1> <input type=hidden name=rsv_idx value=1> <input type=hidden name=tn value=baidu><span class="bg s_ipt_wr"><input id=kw name=wd class=s_ipt value maxlength=255 autocomplete=off autofocus=autofocus></span><span class="bg s_btn_wr"><input type=submit id=su value=百度一下 class="bg s_btn" autofocus></span> </form> </div> </div> <div id=u1> <a href=http://news.baidu.com name=tj_trnews class=mnav>新聞</a> <a href=https://www.hao123.com name=tj_trhao123 class=mnav>hao123</a> <a href=http://map.baidu.com name=tj_trmap class=mnav>地圖</a> <a href=http://v.baidu.com name=tj_trvideo class=mnav>視頻</a> <a href=http://tieba.baidu.com name=tj_trtieba class=mnav>貼吧</a> <noscript> <a href=http://www.baidu.com/bdorz/login.gif?login&amp;tpl=mn&amp;u=http%3A%2F%2Fwww.baidu.com%2f%3fbdorz_come%3d1 name=tj_login class=lb>登錄</a> </noscript> <script>document.write('<a + encodeURIComponent(window.location.href+ (window.location.search === " rel="external nofollow"  rel="external nofollow" " ? "?" : "&")+ "bdorz_come=1")+ '" name="tj_login" class="lb">登錄</a>');
                </script> <a href=//www.baidu.com/more/ name=tj_briicon class=bri style="display: block;">更多產(chǎn)品</a> </div> </div> </div> <div id=ftCon> <div id=ftConw> <p id=lh> <a href=http://home.baidu.com>關(guān)于百度</a> <a href=http://ir.baidu.com>About Baidu</a> </p> <p id=cp>&copy;2017 Baidu <a href=http://www.baidu.com/duty/>使用百度前必讀</a>  <a href=http://jianyi.baidu.com/ class=cp-feedback>意見(jiàn)反饋</a> 京ICP證030173號(hào)  <img src=//www.baidu.com/img/gs.gif> </p> </div> </div> </div> </body> </html>

通過(guò)以上的輸出可以看到https代理是通過(guò)CONNECT請(qǐng)求進(jìn)行連接的,中間有發(fā)生ssl的握手過(guò)程,也已經(jīng)正常進(jìn)行了響應(yīng)。

4 給centos設(shè)置代理訪問(wèn)外網(wǎng)

給centos服務(wù)器設(shè)置兩個(gè)http_proxy和https_proxy環(huán)境變量,假設(shè)nginx服務(wù)器的ip為192.168.0.1,那么在命令行執(zhí)行以下兩條命令,即:

export http_proxy="http://192.168.0.1:8080"
export https_proxy="https://192.168.0.1:8080"

然后就可以順暢地進(jìn)行yum了。當(dāng)然,如果可以的話(huà),就將以上兩條命令配置到bash.rc中,這樣子免得每次登錄都需要敲命令。

到此這篇關(guān)于nginx正向代理https網(wǎng)站的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)nginx正向代理https內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Nginx實(shí)現(xiàn)前端灰度發(fā)布

    Nginx實(shí)現(xiàn)前端灰度發(fā)布

    灰度發(fā)布是一種重要的策略,它允許我們?cè)诓挥绊懰杏脩?hù)的情況下,逐步推出新功能或更新,通過(guò)灰度發(fā)布,我們可以測(cè)試新版本的穩(wěn)定性和性能,下面就來(lái)介紹一下前端灰度發(fā)布的使用,感興趣的可以了解一下
    2025-03-03
  • 解決502?Bad?Gateway錯(cuò)誤的詳細(xì)指南與實(shí)例

    解決502?Bad?Gateway錯(cuò)誤的詳細(xì)指南與實(shí)例

    這篇文章主要給大家介紹了關(guān)于解決502?Bad?Gateway錯(cuò)誤的詳細(xì)指南與實(shí)例,502 Bad Gateway錯(cuò)誤通常是由于網(wǎng)關(guān)或代理服務(wù)器在嘗試訪問(wèn)上游服務(wù)器(通常是Web服務(wù)器)時(shí)未能及時(shí)接收到響應(yīng)導(dǎo)致的,文中將解決辦法介紹的非常詳細(xì),需要的朋友可以參考下
    2024-05-05
  • 前端將項(xiàng)目部署到服務(wù)器(Nginx)的完整步驟

    前端將項(xiàng)目部署到服務(wù)器(Nginx)的完整步驟

    最近寫(xiě)了一個(gè)項(xiàng)目,需要進(jìn)行手機(jī)上測(cè)試,下面就需要前端自己將項(xiàng)目進(jìn)行部署,這篇文章主要給大家介紹了關(guān)于前端將項(xiàng)目部署到服務(wù)器(Nginx)的相關(guān)資料,文中通過(guò)圖文介紹的非常詳細(xì),需要的朋友可以參考下
    2023-01-01
  • 使用Nginx實(shí)現(xiàn)HTTPS雙向驗(yàn)證的方法

    使用Nginx實(shí)現(xiàn)HTTPS雙向驗(yàn)證的方法

    這篇文章主要介紹了使用Nginx實(shí)現(xiàn)HTTPS雙向驗(yàn)證的方法,涉及到單向驗(yàn)證和雙向驗(yàn)證的區(qū)別介紹,本文介紹的非常詳細(xì),具有參考借鑒價(jià)值,感興趣的朋友一起學(xué)習(xí)吧
    2016-06-06
  • centos服務(wù)器中配置nginx的方法示例

    centos服務(wù)器中配置nginx的方法示例

    這篇文章主要介紹了centos服務(wù)器中配置nginx的方法示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-06-06
  • 基于域名、端口和IP搭建nginx虛擬主機(jī)

    基于域名、端口和IP搭建nginx虛擬主機(jī)

    本文給大家分享基于域名、端口和IP搭建nginx虛擬主機(jī)的內(nèi)容,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友參考下吧
    2019-11-11
  • Nginx如何實(shí)現(xiàn)pathinfo模式的方法詳解

    Nginx如何實(shí)現(xiàn)pathinfo模式的方法詳解

    pathinfo是偽靜態(tài)的一種,對(duì)于用過(guò)thinkphp的朋友們來(lái)說(shuō)應(yīng)該都不陌生,下面這篇文章主要給大家介紹了關(guān)于Nginx如何實(shí)現(xiàn)pathinfo模式的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下。
    2017-09-09
  • nginx請(qǐng)求時(shí)找路徑問(wèn)題解決

    nginx請(qǐng)求時(shí)找路徑問(wèn)題解決

    當(dāng)你安裝了nginx的時(shí)候,為nginx配置了如下的location,想要去訪問(wèn)路徑下面的內(nèi)容,可是總是出現(xiàn)404,找不到文件,這是什么原因呢,今天我們就來(lái)解決這個(gè)問(wèn)題,感興趣的朋友一起看看吧
    2023-10-10
  • Nginx反向代理入門(mén)實(shí)戰(zhàn)指南

    Nginx反向代理入門(mén)實(shí)戰(zhàn)指南

    反向代理:反向代理也叫reverse proxy,指的是代理外網(wǎng)用戶(hù)的請(qǐng)求到內(nèi)部的指定web服務(wù)器,并將數(shù)據(jù)返回給用戶(hù)的一種方式,這是用的比較多的一種方式,下面這篇文章主要給大家介紹了關(guān)于Nginx反向代理的相關(guān)資料,需要的朋友可以參考下
    2021-08-08
  • 瀏覽器控制臺(tái)報(bào)錯(cuò)Failed to load module script:解決方法

    瀏覽器控制臺(tái)報(bào)錯(cuò)Failed to load module script:解決方

    這篇文章主要為大家介紹了瀏覽器控制臺(tái)報(bào)錯(cuò)Failed to load module script:解決方法,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-11-11

最新評(píng)論