本文為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






