💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、豆包、星火、月之暗面及文生图、文生视频 广告
正向代理是代理了用户,反向代理是代理了服务器 ## **一、正向代理** 正向代理是指位于客户端和目标服务器之间的代理服务器,客户端必须明确指定代理服务器地址。客户端发送的请求先经过代理服务器,再由代理服务器转发给目标服务器。这样,客户端可以直接与目标服务器通信,而不需要了解代理服务器背后的[网络](https://cloud.baidu.com/product/et.html)结构。 在Nginx中,要配置正向代理,需要在http或server配置块中添加以下内容: ``` location / { proxy_pass http://目标服务器地址; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; } ``` `proxy_pass`指令:指定了目标服务器的地址, `proxy_set_header`指令:用于设置请求头信息 ![](https://img.kancloud.cn/d7/c2/d7c244199dbb5aa132fde68208f2fa24_349x269.png) ![](https://img.kancloud.cn/8f/21/8f21afba5393fafbf43e94a881a612da_703x395.png) ![](https://img.kancloud.cn/14/8e/148e0c7513510b622ca0b3904b73f5f2_984x536.png) 目标是gooogle服务器 电脑是客户端 ## **二、反向代理** 反向代理与正向代理相反,它是将客户端的请求转发给后端服务器,并将后端服务器的响应返回给客户端。客户端不需要了解后端服务器的存在,反向代理服务器充当了客户端和后端服务器之间的桥梁。 在Nginx中,要配置反向代理,需要在http或server配置块中添加以下内容: ``` location / { proxy_pass http://后端服务器地址; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; } ``` `proxy_pass`指令:指定了后端服务器的地址, `proxy_set_header`指令:用于设置请求头信息。 ![](https://img.kancloud.cn/94/66/9466e2083fd99474b28d007cceef5aee_777x440.png) ![](https://img.kancloud.cn/b4/8f/b48fb12b2456c48b71a1a4054ef5fb4f_959x438.png) ### **1、反向代理实例一** 实现效果:使用 Nginx 反向代理,访问`http://www.123.com`直接跳转到 127.0.0.1:8080。 > 注意:此处如果要想从[http://www.123.com](https://link.zhihu.com/?target=http%3A//www.123.com)跳转到本机指定的ip,需要修改本机的hosts文件。`192.168.17.129 www.123.com` 配置代码 ~~~text server { listen 80; server_name 192.168.17.129; location / { root html; index index.html index.htm; proxy_pass http://127.0.0.1:8080 } } ~~~ 如上配置,我们监听 80 端口,访问域名为`http://www.123.com`(不加端口号时默认为 80 端口),故访问该域名时会跳转到 127.0.0.1:8080 路径上。 >[info] 此处的意思为:nginx 反向代理服务监听 192.168.17.129的80端口,如果有请求过来,则转到proxy\_pass配置的对应服务器上,仅此而已。 ``` location / { proxy_pass http://127.0.0.1:48081;//浏览器的请求转发到本机的48081端口 proxy_set_header Host $host;//浏览器输入的地址会自动忽略端口,如浏览器是http://www.123.com:88则$host的值是www.123.com,浏览器是http://192.168.17.129:88则$host的值是192.168.17.129 proxy_set_header X-Real-IP $remote_addr;//客户端(浏览器)的真实 IP 地址,传递给后端服务器 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header REMOTE-HOST $remote_addr; add_header X-Cache $upstream_cache_status; proxy_set_header X-Host $host:$server_port;//如果请求是通过http://www.123.com:88则$server_port是88;,所以`X-Host`会被设置为`www.123.com:88` proxy_set_header X-Scheme $scheme;//表示请求使用的协议(例如http或https) proxy_connect_timeout 30s; proxy_read_timeout 86400s; proxy_send_timeout 30s; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; } ``` ### **2、反向代理实例二** 实现效果:使用 Nginx 反向代理,根据访问的路径跳转到不同端口的服务中,Nginx 监听端口为 9001。 * 访问`http://127.0.0.1:9001/edu/`直接跳转到 127.0.0.1:8081 * 访问`http://127.0.0.1:9001/vod/`直接跳转到 127.0.0.1:8082 第一步,需要准备两个 tomcat,一个 8001 端口,一个 8002 端口,并准备好测试的页面 第二步,修改 nginx 的配置文件,在 http 块中配置 server ~~~text server { listen 9001; server_name 192.168.17.129; location ~ /edu/ { proxy_pass http://127.0.0.1:8080 } location ~ /vod/ { proxy_pass http://127.0.0.1:8081 } } ~~~ 根据上面的配置,当请求到达 Nginx 反向代理服务器时,会根据请求进行分发到不同的服务上。 [Nginx配置反向代理,一篇搞定! - 知乎 (zhihu.com)](https://zhuanlan.zhihu.com/p/451825018) ### **宝塔反向代理** ![](https://img.kancloud.cn/5f/e0/5fe03dd54f7bc62f7545eabbb81660e9_1606x1368.png) ~~~ #PROXY-START/ location ^~ / { proxy_pass http://127.0.0.1:8324; proxy_http_version 1.1; proxy_read_timeout 360s; proxy_redirect off; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; 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_set_header REMOTE-HOST $remote_addr; add_header X-Cache $upstream_cache_status; #Set Nginx Cache set $static_fileLzXnun8E 0; if ( $uri ~* "\.(gif|png|jpg|css|js|woff|woff2)$" ) { set $static_fileLzXnun8E 1; expires 12h; } if ( $static_fileLzXnun8E = 0 ) { add_header Cache-Control no-cache; } } #PROXY-END/ ~~~