一、Nginx正向代理介紹及配置(需要在客戶端配置代理服務器進行指定網站訪問)
#模塊 ngx_http_proxy_module:
http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_set_header
1、環境介紹
代理服務器系統環境為:centos
nginx代理服務器為:192.168.10.10
測試客戶端為局域網內任意windows電腦或linux電腦
2、正向代理簡介
通過代理服務器來訪問服務器的過程 就叫 正向代理。(常見示例,通過正向代理進行上網功能)
3、nginx正向代理的配置
3.1 http 80端口訪問
3.2 https 443端口訪問。
一個處理HTTP轉發,另一個處理HTTPS轉發,而客戶端都通過HTTP來訪問代理,通過訪問代理不同的端口,來區分HTTP和HTTPS請求。
##/usr/local/nginx/conf/nginx.conf
server {
resolver 114.114.114.114; #resolver 定義域名解析。改成一個不存在的ip都不影響。
listen 80;
resolver_timeout 5s; #用于設置DNS服務器域名解析超時時間
access_log /usr/local/openresty/nginx/logs/access.log;
error_log /usr/local/openresty/nginx/logs/error.log;
location / {
proxy_redirect off;
proxy_pass http://$host$request_uri; #設定代理服務器的協議和地址
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_buffers 256 4k; #配置緩存大小,關閉磁盤緩存讀寫減少I/O,以及代理連接超時時間。
proxy_max_temp_file_size 0k;
proxy_connect_timeout 30;
proxy_send_timeout 60;
proxy_read_timeout 60;
proxy_next_upstream error timeout invalid_header http_502;
proxy_cache_valid 200 302 10m; #配置代理服務器 Http 狀態緩存時間。
proxy_cache_valid 301 1h;
proxy_cache_valid any 1m;
client_max_body_size 100m;
client_body_buffer_size 128k;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
proxy_ignore_client_abort on;
}
}
server {
resolver 114.114.114.114; #指定DNS服務器IP地址
listen 443;
resolver_timeout 5s;
access_log /usr/local/openresty/nginx/logs/access.log;
error_log /usr/local/openresty/nginx/logs/error.log;
location / {
proxy_pass https://$http_host$request_uri; #設定代理服務器的協議和地址
proxy_buffers 256 4k;
proxy_max_temp_file_size 0k;
proxy_connect_timeout 30;
proxy_send_timeout 60;
proxy_read_timeout 60;
proxy_next_upstream error timeout invalid_header http_502;
}
}
# /usr/local/nginx/sbin/nginx -s reload
4、Linux客戶端訪問測試
#http的訪問測試
# curl -I --proxy 192.168.10.10:80 www.baidu.com
HTTP/1.1 200 OK
Server: nginx/1.12.1
Date: Mon, 11 Jun 2018 15:37:47 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Thu, 31 May 2018 09:28:16 GMT
Connection: keep-alive
ETag: "5b0fc030-264"
Accept-Ranges: bytes
https的訪問測試
# curl -I --proxy 192.168.10.10:443 www.baidu.com
HTTP/1.1 200 OK
Server: nginx/1.12.1
Date: Mon, 11 Jun 2018 15:38:07 GMT
Content-Type: text/html
Content-Length: 277
Connection: keep-alive
Accept-Ranges: bytes
Cache-Control: private, no-cache, no-store, proxy-revalidate, no-transform
Etag: "575e1f5c-115"
Last-Modified: Mon, 13 Jun 2016 02:50:04 GMT
Pragma: no-cache
5、設置Linux客戶端全局代理
# vim /etc/profile
export http_proxy='192.168.10.10:80'
export http_proxy='192.168.10.10:443'
export ftp_proxy='192.168.10.10:80'
# source /etc/profile
# curl -I www.baidu.com:80
HTTP/1.1 200 OK
Server: nginx/1.12.1
Date: Mon, 11 Jun 2018 16:10:18 GMT
Content-Type: text/html
Content-Length: 277
Connection: keep-alive
Accept-Ranges: bytes
Cache-Control: private, no-cache, no-store, proxy-revalidate, no-transform
Etag: "575e1f5c-115"
Last-Modified: Mon, 13 Jun 2016 02:50:04 GMT
Pragma: no-cache
# curl -I www.baidu.com:443
HTTP/1.1 200 OK
Server: nginx/1.12.1
Date: Mon, 11 Jun 2018 16:10:27 GMT
Content-Type: text/html
Content-Length: 277
Connection: keep-alive
Accept-Ranges: bytes
Cache-Control: private, no-cache, no-store, proxy-revalidate, no-transform
Etag: "575e1f59-115"
Last-Modified: Mon, 13 Jun 2016 02:50:01 GMT
Pragma: no-cache
上面結果就說明我們的服務端nginx正向代理和客戶端使用nginx做為全局代理設置成功。
6、取消代理unset http_proxy