nginx configuration example - Reverse Proxy

Examples of a reverse proxy 

Virtual machine IP: 192.168.116.129
achieve the effect of: using nginx reverse proxy, visit www.123.com jump directly to a virtual machine 192.168.116.129 : 8080 

Experiment Code 

1) Start a tomcat, in the browser address bar enter 192.168.116.129 : 8080, the following interface 

2) by modifying the local host file to map www.123.com to 192.168.116.129

After configuration is complete, we will be able to access the first step in the initial screen that appears Tomcat by www.123.com:8080. So how do you only need to enter www.123.com can jump to Tomcat initial interface it? We use nginx reverse proxy.

3) increase follows the profile nginx.conf 

Note: After modifying the configuration file, need to restart nginx

As configured, we monitor port 80, access to the domain name www.123.com, the default is 80 port when no port number, it will jump to the 127.0.0.1:8080 path to access the domain. Www.123.com input in the browser as follows:
 

Example Two Reverse Proxy 

Virtual machine ip: 192.168.116.129

To achieve the effect: use nginx reverse proxy, jump to a different service ports accessed in the path of nginx listening port 9001,

Access HTTP: // 192.168.116.129 : 9001 / EDU / jump directly to 192.168.116.129. 0.0.1: 8080

访问 http://192.168.116.129:9001/vod/ 直接跳转到 192.168.116.129:8082 

 

实验代码 


1、准备工作

(1)准备两个 tomcat 服务器,一个 8080 端口,一个 8082 端口

(2)创建文件夹和测试页面 

2、具体配

修改 nginx 的配置文件

在 http 块中添加 server{} 


    
    
  1. server{
  2. listen 9001;
  3. server_name localhost;
  4. location ~ /edu/ {
  5. proxy_pass http://localhost:8080;
  6. }
  7. location ~ /dev/ {
  8. proxy_pass http://localhost:8082;
  9. }
  10. }

重启

 

location 指令说明 

语法如下:

  •  1、= :用于不含正则表达式的 uri 前,要求请求字符串与 uri 严格匹配,如果匹配 成功,就停止继续向下搜索并立即处理该请求。 
  •   2、~:用于表示 uri 包含正则表达式,并且区分大小写。 
  •   3、~*:用于表示 uri 包含正则表达式,并且不区分大小写。 
  •   4、^~:用于不含正则表达式的 uri 前,要求 Nginx 服务器找到标识 uri 和请求字 符串匹配度最高的 location 后,立即使用此 location 处理请求,而不再使用 location 块中的正则 uri 和请求字符串做匹配。 
  •   注意:如果 uri 包含正则表达式,则必须要有 ~ 或者 ~* 标识。 

我改了一行配置,会实现下面修改。

有兴趣的朋友可以试试(猜猜我改的那个地方)

 

Guess you like

Origin www.cnblogs.com/heian99/p/11961932.html