一、nginx正向代理介紹及配置
1、環境介紹#
代理服務器系統環境為:centos
nginx代理服務器為:192.168.10.10
測試客戶端為局域網內任意windows電腦或Linux電腦
2、正向代理簡介#
nginx不僅可以做反向代理,還能用作正向代理來進行上網等功能。如果把局域網外的Internet想象成一個巨大的資源庫,則局域網中的客戶端要訪問Internet,則需要通過代理服務器來訪問,這種代理服務就稱為正向代理(也就是大家常說的,通過正向代理進行上網功能)
3、nginx正向代理的配置#
現在的網站基本上都是https,要解決既能訪問http80端口也能訪問https443端口的網站,需要配置兩個SERVER節點,一個處理HTTP轉發,另一個處理HTTPS轉發,而客戶端都通過HTTP來訪問代理,通過訪問代理不同的端口,來區分HTTP和HTTPS請求。
[root@localhost ~]# vim /usr/local/nginx-1.12.1/conf/nginx.conf server { resolver 114.114.114.114; #指定DNS服務器IP地址 listen 80; location / { proxy_pass http://$host$request_uri; #設定代理服務器的協議和地址 proxy_set_header HOST $host; 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; } } server { resolver 114.114.114.114; #指定DNS服務器IP地址 listen 443; location / { proxy_pass https://$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; } } [root@localhost ~]# /usr/local/nginx-1.12.1/sbin/nginx -s reload
4、Linux客戶端訪問測試
http的訪問測試
[root@localhost ~]# 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的訪問測試 [root@localhost ~]# 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客戶端全局代理 [root@localhost ~]# 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' [root@localhost ~]# source /etc/profile [root@localhost ~]# 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 [root@localhost ~]# 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