tengine、nginx配置正向代理,其他內網機器通過代理訪問外網,支援https
本文為joshua317原創文章,轉載請註明:轉載自joshua317部落格https://www.joshua317.com/article/250
1.進入軟體包目錄
cd /usr/local/src
2.下載tengine
wget https://tengine.taobao.org/download/tengine-2.3.3.tar.gz
3.解壓
tar zxvf tengine-2.3.3.tar.gz
4.更新升級apt-get
apt-get update
apt-get upgrade
5.安裝依賴庫
5.1 PCRE 庫
PCRE(Perl Compatible Regular Expressions)是一個 Perl 庫,包括 perl 相容的正則表示式庫。nginx rewrite 依賴於 PCRE 庫,所以在安裝 Tengine 前一定要先安裝 PCRE。
apt-get install libpcre3 libpcre3-dev
5.2 Zlib 庫
Zlib 是提供資料壓縮用的函式庫,當 Tengine 想啟用 gzip 壓縮的時候就需要使用到 Zlib。
apt-get install zlib1g-dev
5.3 OpenSSL 庫
OpenSSL 是一個強大的安全套接字層密碼庫,囊括主要的密碼演算法、常用的金鑰和證書封裝管理功能及 SSL 協議,並提供豐富的應用程式供測試或其它目的使用。安裝 OpenSSL 主要是為了讓 Tengine 支援 HTTPS 的訪問請求。
apt-get install openssl libssl-dev
6.安裝build-essential
解決:./configure: error: C compiler cc is not found
apt-get install build-essential
7.生成makefile
此處增加了proxy_connect模組,用來支援代理服務支援https的請求,從而可以實現內網機器通過代理訪問外網。
./configure --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --add-module=./modules/ngx_http_proxy_connect_module
8.編譯安裝
make && make install
9.開機自啟動
vi /lib/systemd/system/nginx.service
Description=nginx - high performance web server
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
[Install]
WantedBy=multi-user.target
10.使配置生效
systemctl daemon-reload
注意可能會產生下面的錯誤
錯誤:System has not been booted with systemd as init system (PID 1). Can‘t operate. 原因:如果是一般的Linux作業系統,可能是因為Linux中沒有使用systemd,想用systemd命令來管理Linux上的服務,因此會報錯,很可能是使用的是經典的SysV init(sysvinit)系統。 但我這個是window10下WSL的Ubuntu,就會使SysV init而不是systemd。 解決:apt install systemctl
11.設定開機啟動
systemctl enable nginx.service
12、執行tengine
1)啟動
/usr/local/nginx/sbin/nginx
或者
systemctl start nginx.service
2)重啟
/usr/local/nginx/sbin/nginx -s reload
或者
systemctl reload nginx.service
3)停止
/usr/local/nginx/sbin/nginx -s stop
或者
systemctl stop nginx.service
13.是否啟動成功,預設80埠,訪問地址:http://ip:port/
14.配置nginx
參考文件:https://tengine.taobao.org/document_cn/proxy_connect_cn.html
server {
listen 3182;
# dns resolver used by forward proxying
resolver 114.114.114.114;
# forward proxy for CONNECT request
proxy_connect;
proxy_connect_allow 443 563;
proxy_connect_connect_timeout 10s;
proxy_connect_read_timeout 10s;
proxy_connect_send_timeout 10s;
# forward proxy for non-CONNECT request
location / {
proxy_pass $scheme://$http_host$request_uri;
proxy_set_header Host $host;
}
}
15 重啟nginx
#校驗nginx.conf配置檔案
/usr/local/nginx/sbin/nginx -t
#校驗通過,重啟nginx
/usr/local/nginx/sbin/nginx -s reload
16.測試
通過代理訪問git
curl https://www.baidu.com/ -v -x 127.0.0.1:3182
curl -i --proxy 127.0.0.1:3182 www.baidu.com
17.內網機器通過代理訪問到外網服務
17.1 windows系統下
開啟IE瀏覽器,在IE設定中新增代理訪問,工具-》Internet設定-》連線--》區域網(LAN)設定
設定完,開啟瀏覽器進行測試即可。可以通過nginx的訪問日誌進行檢視。
17.2 linux系統下
在內網的機器上進行操作:
vi /etc/profile
#export http_proxy=正向代理伺服器http的IP:埠
export http_proxy=192.168.3.114:3182
#export https_proxy=正向代理伺服器https的IP:埠
export https_proxy=192.168.3.114:3182
#儲存檔案後重新載入環境
source /etc/profile
其中192.168.3.114是安裝nginx代理伺服器的ip。3182是代理服務的埠號。
測試:
curl -i www.baidu.com
#如果未加環境變數代理設定,則可以通過臨時代理訪問
curl -i --proxy 192.168.3.114:3182 www.baidu.com
18.擴充套件
18.1 Nginx Http Proxy 代理伺服器,本身是不支援代理 Https 網站。
因為 Nginx 不支援 CONNECT,所以無法正向代理 Https 網站。如果訪問 Https 網站,比如:https://www.baidu.com,Nginx access.log 日誌如下:
"CONNECT www.baidu.com:443 HTTP/1.1" 400
所以想要代理Https 網站,需要讓nginx支援 CONNECT模式,即增加ngx_http_proxy_connect_module模組。
18.2 ngx_http_proxy_connect_module的文件
ngx_http_proxy_connect_module模組主要用於隧道SSL請求的代理伺服器。
文件地址:https://github.com/chobits/ngx_http_proxy_connect_module#proxy_connect
本文為joshua317原創文章,轉載請註明:轉載自joshua317部落格https://www.joshua317.com/article/250