Nginx-location + proxy_pass详解

一、环境

名称 IP 服务
DMZ区Nginx 10.22.86.201 nginx代理
内网前端服务 10.22.86.202 前端服务

简化实验,这里都在DMZ区做代理。由DMZ区服务器的一个端口访问,代理到DMZ区服务器的另一个端口,访问前端服务。

[root@localhost test]# mkdir -p /data/test
[root@localhost test]# echo "1" > /data/test/index.html

二、配置文件。

1、情景1

# nginx 配置文件内容
server {
    listen       8801;
    server_name  10.22.86.201;
    location /test/test1/ {
        proxy_pass http://10.22.86.201:8802/test/test1/;
    }
}

server {
    listen       8802;
    server_name  10.22.86.201;
    location /test/test1/ {
        root  /data/test/;
        try_files $uri $uri/ /index.html =404;
        index  index.html index.htm;
    }
}

此配置文件情况下,地址如下:

# 正常访问
http://10.22.86.201:8801/test/test1/

# 正常访问
http://10.22.86.201:8801/test/test1/dadasdada

# 正常访问
http://10.22.86.201:8801/test/test1/ + 后面任意字符

# 访问失败
http://10.22.86.201:8801/test/testada

2、情景2

# nginx 配置文件内容
server {
    listen       8801;
    server_name  10.22.86.201;
    location /test/test1 {
        proxy_pass http://10.22.86.201:8802/test/test1/;
    }
}

server {
    listen       8802;
    server_name  10.22.86.201;
    location /test/test1/ {
        root  /data/test/;
        try_files $uri $uri/ /index.html =404;
        index  index.html index.htm;
    }
}
posted @ 2022-02-18 14:42  Rocky_940120  阅读(287)  评论(0)    收藏  举报