Nginx -- 08 -- location、proxy_pass配置解析

当我们在 Nginx 中配置代理转发 location、proxy_pass 时,经常会碰到 url 有加 /不加 / 的情况,让人十分的困惑,这里我们就来分析下加 / 和不加 / 究竟有什么区别


一、部署项目

首先我用 IDEA 简单地搭建了一个 SpringBoot 项目,使用了 freemark 作为模板引擎

  • pom.xml

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-freemarker</artifactId>
        </dependency>
    </dependencies>
    
  • application.yml

    server:
      port: 9000
    
  • NginxController.java

    /**
     * @Title: NginxController.java
     * @Description: Nginx Controller层
     * @Author: xj
     * @Date: 2019/1/25 17:22
     */
    @Controller
    public class NginxController {
    
        private static final Logger logger = LoggerFactory.getLogger(NginxController.class);
    
        @GetMapping(value = "/test")
        public String test1() {
            logger.info("I'm test1,my access path is 【{}】", "/test");
            return "nginx";
        }
    
        @GetMapping(value = "/proxy/test")
        public String test2() {
            logger.info("I'm test2,my access path is 【{}】", "/proxy/test");
            return "nginx";
        }
    
        @GetMapping(value = "/nginx/test")
        public String test3() {
            logger.info("I'm test3,my access path is 【{}】", "/nginx/test");
            return "nginx";
        }
    
        @GetMapping(value = "/proxytest")
        public String test4() {
            logger.info("I'm test4,my access path is 【{}】", "/proxytest");
            return "nginx";
        }
    
        @GetMapping(value = "/nginxtest")
        public String test5() {
            logger.info("I'm test5,my access path is 【{}】", "/nginxtest");
            return "nginx";
        }
    }
    
  • nginx.ftl

    <html>
        <body>
            <h1>我是一个页面!!!</h1>
        </body>
    </html>
    

搭建完项目后,我们将其打包部署到服务器 (内网服务器) 上,然后运行程序,其端口为 9000


二、测试

接下来我们对 proxy_pass 后加 / 和不加 / 的情形进行相关测试


location 后加 /,proxy_pass 后加 /

  • 端口后加 / (匹配 test1() 方法)

    server {
        listen  7040;
        server_name  127.0.0.1;
    
        location /nginx/ {
            proxy_pass  http://127.0.0.1:9000/;
        }
    }
    

    这种情况下,Nginx 会把 location 中匹配到的部分给代理掉,可以理解为使用 http://192.168.20.210:9000/ 替换掉了 http://192.168.20.210:7040/nginx/

  • 路由后加 / (匹配 test2() 方法)

    server {
        listen  7040;
        server_name  127.0.0.1;
    
        location /nginx/ {
            proxy_pass  http://127.0.0.1:9000/proxy/;
        }
    }
    

    这种情况下,Nginx 会把 location 中匹配到的部也给代理掉,可以理解为使用 http://127.0.0.1:9000/proxy/ 替换掉了 http://192.168.20.210:7040/nginx/


location 后加 /,proxy_pass 后不加 /

  • 端口后不加 / (匹配 test3() 方法)

    server {
        listen  7040;
        server_name  127.0.0.1;
    
        location /nginx/ {
            proxy_pass  http://127.0.0.1:9000;
        }
    }
    

    这种情况下,Nginx 不会把 location 中匹配到的部分给代理掉,可以理解为使用 http://127.0.0.1:9000 替换掉了 http://192.168.20.210:7040

  • 路由后不加 / (匹配 test4() 方法)

    server {
        listen  7040;
        server_name  127.0.0.1;
    
        location /nginx/ {
            proxy_pass  http://127.0.0.1:9000/proxy;
        }
    }
    

    这种情况下,Nginx 会把 location 中匹配到的部分给代理掉,可以理解为使用 http://127.0.0.1:9000/proxy 替换掉了 http://192.168.20.210:7040/nginx/


location 后不加 /,proxy_pass 后加 /

  • 端口后加 /

    server {
        listen  7040;
        server_name  127.0.0.1;
    
        location /nginx {
            proxy_pass  http://127.0.0.1:9000/;
        }
    }
    

    这两种情况下,Nginx 会把 location 中匹配到的部分给代理掉,第一种可以理解为使用 http://192.168.20.210:9000/ 替换掉了 http://192.168.20.210:7040/nginx/;第二种可以理解为使用 http://192.168.20.210:9000/ 替换掉了 http://192.168.20.210:7040/nginx

    由上可知,与 location 后加 / 相比,不同之处在于加 / 为全匹配 (只能匹配 /nginx/),而不加 / 为模糊匹配 (既可以匹配 /nginx/,也可以匹配 /nginx*)

  • 路由后加 /

    server {
        listen  7040;
        server_name  127.0.0.1;
    
        location /nginx {
            proxy_pass  http://127.0.0.1:9000/proxy/;
        }
    }
    

    这两种情况下,Nginx 会把 location 中匹配到的部分给代理掉,第一种可以理解为使用 http://192.168.20.210:9000/proxy/ 替换掉了 http://192.168.20.210:7040/nginx/;第二种可以理解为使用 http://192.168.20.210:9000/proxy/ 替换掉了 http://192.168.20.210:7040/nginx

    由上可知,与 location 后加 / 相比,不同之处在于加 / 为全匹配 (只能匹配 /nginx/),而不加 / 为模糊匹配 (既可以匹配 /nginx/,也可以匹配 /nginx*)


location 后不加 /,proxy_pass 后不加 /

  • 端口后不加 /

    server {
        listen  7040;
        server_name  127.0.0.1;
    
        location /nginx {
            proxy_pass  http://127.0.0.1:9000;
        }
    }
    

    这两种情况下,Nginx 不会把 location 中匹配到的部分给代理掉,两种都可以理解为使用 http://127.0.0.1:9000 替换掉了 http://192.168.20.210:7040

    由上可知,与 location 后加 / 相比,不同之处在于加 / 为全匹配 (只能匹配 /nginx/),而不加 / 为模糊匹配 (既可以匹配 /nginx/,也可以匹配 /nginx*)

  • 路由后不加 /

    server {
        listen  7040;
        server_name  127.0.0.1;
    
        location /nginx {
            proxy_pass  http://127.0.0.1:9000/proxy;
        }
    }
    

    这两种情况下,Nginx 会把 location 中匹配到的部分给代理掉,两种都可以理解为使用 http://127.0.0.1:9000/proxy 替换掉了 http://192.168.20.210:7040/nginx

    由上可知,与 location 后加 / 相比,不同之处在于加 / 为全匹配 (只能匹配 /nginx/),而不加 / 为模糊匹配 (既可以匹配 /nginx/,也可以匹配 /nginx*)

posted @ 2019-05-08 14:45  GeneXu  阅读(188)  评论(0)    收藏  举报