(一)簡述
nginx不只能夠作反向代理,還能用做正向代理來進行上網等功能。正向代理:若是把局域網外的Internet想象成一個巨大的資源庫,則局域網中的客戶端要訪問Internet,則須要經過代理服務器來訪問,這種代理服務就稱爲正向代理(也就是你們常說的,經過正向代理進行上網功能)html
(二)nginx正向代理的功能
1.因爲nginx正向代理的功能指令較少,只須要進行簡單的配置便可nginx
server { resolver 114.114.114.114; #指定DNS服務器IP地址 listen 8080; location / { proxy_pass http://$http_host$request_uri; #設定代理服務器的協議和地址 } }
以上的配置只能訪問80 端口的網站,而不能訪問https443端口的網站,如今的網站基本上都是https的要解決技能訪問http80端口也能訪問https443端口的網站,須要置兩個SERVER節點,一個處理HTTP轉發,另外一個處理HTTPS轉發,而客戶端都經過HTTP來訪問代理,經過訪問代理不一樣的端口,來區分HTTP和HTTPS請求。vim
server { resolver 114.114.114.114; #指定DNS服務器IP地址 listen 80; location / { proxy_pass http://$http_host$request_uri; #設定代理服務器的協議和地址 proxy_set_header HOST $http_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; } }
2.客戶端訪問設置:
2.1 Windows系統:
爲瀏覽器配置http代理便可,具體步驟以下:"打開瀏覽器"->Internet選項」 -> 「鏈接」 -> 「局域網設置」 -> 「代理服務器」,而後設置以下:
瀏覽器
2.2 Linux訪問地址以下:
若是訪問HTTP網站,能夠直接這樣的方式: curl --proxy proxy_server:80 http://www.taobao.com/
若是訪問HTTPS網站,例如https://www.alipay.com,那麼可使用nginx的HTTPS轉發的server:
curl --proxy proxy_server:443 http://www.alipay.com安全
###經過http的訪問 [root@localhost ~]# curl -I --proxy 192.168.99.99:80 www.baidu.com ###顯示http訪問的狀態碼 HTTP/1.1 200 OK Server: nginx/1.12.2 Date: Wed, 07 Feb 2018 02:09:03 GMT Content-Type: text/html Content-Length: 277 Connection: keep-alive Last-Modified: Mon, 13 Jun 2016 02:50:40 GMT ETag: "575e1f80-115" Cache-Control: private, no-cache, no-store, proxy-revalidate, no-transform Pragma: no-cache Accept-Ranges: bytes [root@localhost ~]# curl --proxy 192.168.99.99:80 www.baidu.com ####顯示http訪問整個網頁
###經過https的訪問 [root@localhost ~]# curl -I --proxy 192.168.99.99:443 http://www.taobao.com/ ### HTTP/1.1 200 OK Server: nginx/1.12.2 Date: Wed, 07 Feb 2018 02:13:14 GMT Content-Type: text/html; charset=utf-8 Connection: keep-alive Vary: Accept-Encoding Vary: Ali-Detector-Type Cache-Control: max-age=60, s-maxage=90 X-Snapshot-Age: 1 Content-MD5: LIH52+3GPE2b2ELlP/CffQ== ETag: W/"295b-1616605047e" Via: cache47.l2cn624[14,304-0,C], cache29.l2cn624[11,0], cache2.cn12[0,200-0,H], cache5.cn12[0,0] Age: 17 X-Cache: HIT TCP_MEM_HIT dirn:26:913567405 mlen:-1 X-Swift-SaveTime: Wed, 07 Feb 2018 02:12:57 GMT X-Swift-CacheTime: 90 Timing-Allow-Origin: * EagleId: 7250ae1715179695945657582e Set-Cookie: thw=cn; Path=/; Domain=.taobao.com; Expires=Thu, 07-Feb-19 02:13:14 GMT; Strict-Transport-Security: max-age=31536000 [root@localhost ~]# curl --proxy 192.168.99.99:443 http://www.taobao.com/ ###經過https代理訪問
(三)Linux設置代理上網的方法:
線上環境爲了安全期間,服務器是不允許上外網的,若是須要經過yum來進行更新或下載相應的軟件包就比較麻煩,如今能夠經過設置代理的方式來進行上外網的操做,具體方法以下:服務器
(1)使用yum 的設置代理的方法。若是隻須要使用yum來更新包的,只需進行yum配置便可。curl
[root@localhost ~]# vim /etc/yum.conf proxy=http://192.168.99.99:80 proxy=ftp://192.168.99.99:80 #proxy_username=username #####代理的用戶名 #proxy_password=password #####代理的密碼 [root@localhost ~]# yum install iotop -y Loaded plugins: fastestmirror base | 3.6 kB 00:00:00 extras | 3.4 kB 00:00:00 updates | 3.4 kB 00:00:00 Loading mirror speeds from cached hostfile Resolving Dependencies --> Running transaction check ---> Package iotop.noarch 0:0.6-2.el7 will be installed --> Finished Dependency Resolution Dependencies Resolved ======================================================================================================================================== Package Arch Version Repository Size ======================================================================================================================================== Installing: iotop noarch 0.6-2.el7 base 52 k Transaction Summary ======================================================================================================================================== Install 1 Package Total download size: 52 k Installed size: 156 k Downloading packages: iotop-0.6-2.el7.noarch.rpm | 52 kB 00:00:00 Running transaction check Running transaction test Transaction test succeeded Running transaction Installing : iotop-0.6-2.el7.noarch 1/1 Verifying : iotop-0.6-2.el7.noarch 1/1 Installed: iotop.noarch 0:0.6-2.el7 Complete!
***備註:vi /etc/yum.conf***
添加下面內容
proxy = http://username:password@yourproxy:8080/
或者
proxy=http://yourproxy:808
proxy=ftp://yourproxy:808
proxy_username=username
proxy_password=passwordide
2.wget設置代理的方法:
[root@localhost ~]# vim /etc/wgetrc
http_proxy=192.168.99.99:80
http_proxy=192.168.99.99:443網站
3.curl訪問代理設置的方法:url
若是訪問HTTP網站,能夠直接這樣的方式: curl --proxy proxy_server:80 http://www.taobao.com/ 若是訪問HTTPS網站,例如https://www.alipay.com,那麼可使用nginx的HTTPS轉發的server: curl --proxy proxy_server:443 http://www.alipay.com [root@localhost ~]# curl -I --proxy 192.168.99.99:80 www.baidu.com ###顯示http訪問的狀態碼 HTTP/1.1 200 OK 備註:上邊有介紹,詳見上邊內容。
4.使用設置全局代理的方法:
[root@localhost ~]# vim /etc/profile http_proxy = http://192.168.99.99:80 http_proxy = http://192.168.99.99:443 ftp_proxy = http://192.168.99.99:80/ export http_proxy export ftp_proxy
[root@localhost ~]# curl -I https://www.taobao.comHTTP/1.1 200 OKServer: TengineDate: Wed, 07 Feb 2018 02:50:49 GMTContent-Type: text/html; charset=utf-8Connection: keep-aliveVary: Accept-EncodingVary: Ali-Detector-TypeCache-Control: max-age=60, s-maxage=90X-Snapshot-Age: 2Content-MD5: p7MoaH71PI2hqTQ/lcsW4Q==ETag: W/"295b-1616605047e"Via: cache40.l2et15-1[0,304-0,H], cache20.l2et15-1[0,0], cache10.cn418[0,200-0,H], cache5.cn418[1,0]Age: 22X-Cache: HIT TCP_MEM_HIT dirn:25:104405114 mlen:-1X-Swift-SaveTime: Wed, 07 Feb 2018 02:50:27 GMTX-Swift-CacheTime: 90Timing-Allow-Origin: *EagleId: 65e3d1e515179718498223532eSet-Cookie: thw=cn; Path=/; Domain=.taobao.com; Expires=Thu, 07-Feb-19 02:50:49 GMT;Strict-Transport-Security: max-age=31536000