Nginx Tutorial Reverse Proxy

"Nginx is a very powerful high-performance Web and reverse proxy service" is quoted from Baidu Encyclopedia. In fact, Nginx can be used not only as a direction proxy, but also as a forward proxy server. Next, let's take a look at these two proxy methods.

forward proxy

concept

A proxy server (intermediate server) that sits between the client and the target server. To get content from the original server, the client sends a request to the proxy server, specifying the target server, and the proxy forwards to the target server and returns the obtained content to the client. In the case of a forward proxy, the client must perform some special settings before it can be used. Common scenarios such as VPN.

relation

Client <1> Proxy 1> Server . Note the red arrow, the forward proxy is the proxy to the client. The target service is clear. A simple example: A (client) wants to rent the house of C (server), but A (client) does not know C (server) and cannot rent it. B (agent) knows that C (server) can rent this house, so you asked B (agent) to help you rent this house. In this process, C (server) does not know A (client) but only knows B (agent). C (server) does not know that A (client) has rented a house, but only knows that the house is rented to B (agent).

reverse proxy

concept

Just the opposite of positive. To the client, the reverse proxy looks like the target server. And the client does not need to make any settings. The client sends a request to the reverse proxy, and then the reverse proxy determines where the request is going, and forwards the request to the client, making the content just like itself, and once the client does not perceive the service behind the reverse proxy , so the client does not need to make any settings, just treat the reverse proxy server as a real server. Common scenarios include domain name proxying multiple ports, using domain name proxying multiple server applications, and so on.

relation

client one> proxy <one> server. Note the red arrow, the forward proxy is the proxy to the server. Also give an example: A (client) wants to rent a house, and B (agent) rents the house to him. At this time, C (server) is actually the landlord. B (agent) is the intermediary who rented the house to A (client). In this process, A (client) does not know who is the landlord of this house, and he may think that this house belongs to B (agent).

反向代理实战

实战一:直接用域名代理目标地址

使用 nginx 反向代理,访问 http://springboot.com/ 直接跳转到 127.0.0.1:8080。

环境准备:springboot服务,docker,mac。我采用的是docker nginx挂载文件修改,大家想看挂载相关的操作可以看看这篇文章。《docker 挂载、修改文件》https://blog.csdn.net/lly576403061/article/details/129507650。直接修改文件如下,重点我已经标注。然后在浏览器输入:http://localhost:8010/demo1/api/demo1/start即可访问。

实战二:反向代理使用不同的路径请求不同的服务

使用Nginx的反向代理,在浏览器访问同一个域名和端口,只是根据访问路径的不同调用相应的服务。具体case如下:

http://localhost:8088/demo1/api/demo1/start 访问微服务demo1的相关接口。

http://localhost:8088/demo2/api/demo2/start 访问微服务demo2的相关接口。

具体操作之前咱们在《Docker Nginx 反向代理》里有聊过,可以看看。https://blog.csdn.net/lly576403061/article/details/129452986

番外:Location指令的匹配说明

location,该指令用于匹配 URL,具体测说明如下。

location [ = | ~ | ~* | ^~ ] uri { ... }
  1. = :用于不含正则表达式的 uri 前,要求请求字符串与 uri 严格匹配,如果匹配成功,就停止继续向下搜索并立即处理该请求。

  1. ~:用于表示 uri 包含正则表达式,并且区分大小写。

  1. ~*:用于表示 uri 包含正则表达式,并且不区分大小写。

  1. ^~:用于不含正则表达式的 uri 前,要求 Nginx 服务器找到标识 uri 和请求字符串匹配度最高的 location 后,立即使用此 location 处理请求,而不再使用 location 块中的正则 uri 和请求字符串做匹配。

注意:如果 uri 包含正则表达式,则必须要有 ~ 或者 ~* 标识。

附件:官方文档:https://nginx.org/en/docs/http/ngx_http_core_module.html#location

Guess you like

Origin blog.csdn.net/lly576403061/article/details/129570173