搭建Nginx正向代理
生活随笔
收集整理的這篇文章主要介紹了
搭建Nginx正向代理
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Nginx正向代理和反向代理的區別
- 正向代理
- 正向代理服務器位于客戶端和服務器之間,為了從服務器獲取數據,客戶端要向代理服務器發送一個請求,并指定目標服務器,代理服務器將目標服務器返回的數據轉交給客戶端。這里客戶端需要要進行一些正向代理的設置的。
- 正向代理中被代理的是客戶端的請求
- 反向代理
- 反向代理,客戶端對代理是無感知的,客戶端不需要任何配置就可以訪問,客戶端將請求發送到反向代理服務器,由反向代理服務器去選擇目標服務器獲取數據后,在返回給客戶端,此時反向代理服務器和目標服務器對外就是一個服務器,暴露的是代理服務器地址,隱藏了真實服務器IP地址。
以租房為例解釋正向代理和反向代理:
正向代理:客戶端 <一> 代理 一>服務端
A(客戶端)想租C(服務端)的房子,但是A(客戶端)并不認識C(服務端)租不到。
B(代理)認識C(服務端)能租這個房子所以你找了B(代理)幫忙租到了這個房子。
這個過程中C(服務端)不認識A(客戶端)只認識B(代理)
C(服務端)并不知道A(客戶端)租了房子,只知道房子租給了B(代理)。
反向代理:客戶端 一>代理 <一> 服務端
A(客戶端)想租一個房子,B(代理)就把這個房子租給了他。
這時候實際上C(服務端)才是房東。
B(代理)是中介把這個房子租給了A(客戶端)。
這個過程中A(客戶端)并不知道這個房子到底誰才是房東
他都有可能認為這個房子就是B(代理)的
由上的例子我們可以知道正向代理和反向代理的區別在于代理的對象不一樣,正向代理的代理對象是客戶端,反向代理的代理對象是服務端。
Nginx源碼包下載
環境介紹
此處兩臺主機都是我自己搭建的,一臺可以訪問外網的主機test,第一張網卡是橋接形式,第二張網卡是nat形式,另一臺主機是純內網主機nginx-test,網卡是nat形式,具體如下圖:
[root@test ~]# ifconfig ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500inet 10.0.2.53 netmask 255.255.255.128 broadcast 10.0.2.127inet6 fe80::a01c:6897:deef:8b24 prefixlen 64 scopeid 0x20<link>ether 00:0c:29:a5:25:99 txqueuelen 1000 (Ethernet)RX packets 8406 bytes 754164 (736.4 KiB)RX errors 0 dropped 0 overruns 0 frame 0TX packets 386 bytes 32064 (31.3 KiB)TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0ens37: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500inet 192.168.2.130 netmask 255.255.255.0 broadcast 192.168.2.255inet6 fe80::df59:fe9b:13ca:20c7 prefixlen 64 scopeid 0x20<link>ether 00:0c:29:a5:25:a3 txqueuelen 1000 (Ethernet)RX packets 4999 bytes 1416855 (1.3 MiB)RX errors 0 dropped 0 overruns 0 frame 0TX packets 3156 bytes 550147 (537.2 KiB)TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536inet 127.0.0.1 netmask 255.0.0.0inet6 ::1 prefixlen 128 scopeid 0x10<host>loop txqueuelen 1000 (Local Loopback)RX packets 32 bytes 2592 (2.5 KiB)RX errors 0 dropped 0 overruns 0 frame 0TX packets 32 bytes 2592 (2.5 KiB)TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0[root@test-nginx ~]# ifconfig ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500inet 192.168.2.131 netmask 255.255.255.0 broadcast 192.168.2.255inet6 fe80::651a:7008:c8e3:8858 prefixlen 64 scopeid 0x20<link>ether 00:0c:29:f9:dc:29 txqueuelen 1000 (Ethernet)RX packets 2210 bytes 197945 (193.3 KiB)RX errors 0 dropped 0 overruns 0 frame 0TX packets 1588 bytes 244702 (238.9 KiB)TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536inet 127.0.0.1 netmask 255.0.0.0inet6 ::1 prefixlen 128 scopeid 0x10<host>loop txqueuelen 1000 (Local Loopback)RX packets 32 bytes 2592 (2.5 KiB)RX errors 0 dropped 0 overruns 0 frame 0TX packets 32 bytes 2592 (2.5 KiB)TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0[root@test ~]# wget www.baidu.com # 通外網主機可以正常下載頁面 2020-09-25 14:17:04 (159 MB/s) - 已保存 “index.html” [2250/2250])[root@test-nginx ~]# wget www.baidu.com # 內網主機無法下載 wget: 無法解析主機地址 “www.baidu.com”修改nginx的配置文件
- 通外網主機: [root@test ~]# cd /opt/ [root@test opt]# wget http://nginx.org/download/nginx-1.12.2.tar.gz [root@test opt]# ll 總用量 960 -rw-r--r--. 1 root root 981687 9月 25 13:56 nginx-1.12.2.tar.gz [root@test opt]# yum -y install openssl-devel pcre-devel zlib-devel gcc # 下載依賴包 [root@test opt]# cd nginx-1.12.2/ [root@test nginx-1.12.2]# ./configure --prefix=/usr/local/nginx [root@test nginx-1.12.2]# make && make install [root@test ~]# vim /usr/local/nginx/conf/nginx.conf http { resolver 114.114.114.114; # DNS解析地址include mime.types;default_type application/octet-stream;#log_format main '$remote_addr - $remote_user [$time_local] "$request" '# '$status $body_bytes_sent "$http_referer" '# '"$http_user_agent" "$http_x_forwarded_for"';#access_log logs/access.log main;sendfile on;#tcp_nopush on;#keepalive_timeout 0;keepalive_timeout 65;#gzip on;server { # 增加無server_name名的server和proxy_pass指令listen 8088;location /{proxy_pass http://$http_host$request_uri;}}server {listen 80;server_name localhost; ...[root@test ~]# /usr/local/nginx/sbin/nginx -t # 檢查nginx語法 nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful [root@test ~]# /usr/local/nginx/sbin/nginx # 起服務 [root@test ~]# ss -nutlp | grep 8088 # 查看8088端口是否開放 tcp LISTEN 0 128 *:8088 *:* users:(("nginx",pid=9798,fd=10),("nginx",pid=9770,fd=10)) - 內網主機: 當前用戶永久生效: [root@test-nginx ~]# vim ~/.wgetrc # 創建$HOME/.wgetrc 文件,配置http_proxy=代理主機:端口,配置完后,就可以通過代理 wget 下載包了。 http_proxy=192.168.2.130:8088 [root@test ~]# systemctl stop firewalld.service # 關閉防火墻或者設置防火墻策略 [root@test-nginx ~]# systemctl stop firewalld.service[root@test-nginx ~]# wget www.baidu.com # 此時內網主機就已經可以正常通過代理服務器進行正常的下載頁面了 正在連接 192.168.2.130:8088... 已連接。 已發出 Proxy 請求,正在等待回應... 302 Moved Temporarily 位置:http://1.1.1.3/ac_portal/proxy.html?template=default&tabs=pwd&vlanid=0&url=http://www.baidu.com%2f [跟隨至新的 URL] --2020-09-25 14:55:51-- http://1.1.1.3/ac_portal/proxy.html?template=default&tabs=pwd&vlanid=0&url=http://www.baidu.com%2f 正在連接 192.168.2.130:8088... 已連接。 已發出 Proxy 請求,正在等待回應... 200 OK 長度:2250 (2.2K) [text/html] 正在保存至: “index.html” 2020-09-25 14:55:51 (292 MB/s) - 已保存 “index.html” [2250/2250])全局代理配置: 修改/etc/profile或者./.bash_profile [root@test-nginx ~]# vim /etc/profile http_proxy=192.168.2.130:8088 https_proxy=192.168.2.130:8088 ftp_proxy=192.168.2.130:8088 export http_proxy https_proxy ftp_proxy[root@test-nginx ~]# source /etc/profile踩坑系列
如果出現tar解壓報錯,可能是因為下載壓縮包的鏈接有誤,其實下載的是網頁,我們可以使用file命令進行查看,需要進行重新下載
[root@test opt]# tar -xf nginx-1.12.2.tar.gz gzip: stdin: not in gzip format tar: Child returned status 1 tar: Error is not recoverable: exiting now [root@test opt]# ll 總用量 4 -rw-r--r--. 1 root root 2250 9月 25 11:31 nginx-1.12.2.tar.gz [root@test opt]# file nginx-1.12.2.tar.gz nginx-1.12.2.tar.gz: HTML document, UTF-8 Unicode text[root@test opt]# ll 總用量 960 -rw-r--r--. 1 root root 981687 9月 25 13:56 nginx-1.12.2.tar.gz [root@test opt]# file nginx-1.12.2.tar.gz nginx-1.12.2.tar.gz: gzip compressed data, from Unix, last modified: Tue Oct 17 21:16:44 2017 《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀總結
以上是生活随笔為你收集整理的搭建Nginx正向代理的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 安装zabbix4.0+grafana
- 下一篇: DNS服务器分离解析 RAID阵