Nginx的Location正则表达式

location的作用

  location指令的作用是根据用户请求的URI来执行不同的应用,也就是根据用户请求的网站URL进行匹配,匹配成功即进行相关的操作。

location的语法

已=开头表示精确匹配
如 A 中只匹配根目录结尾的请求,后面不能带任何字符串。

^~ 开头表示uri以某个常规字符串开头,不是正则匹配

~ 开头表示区分大小写的正则匹配;

~* 开头表示不区分大小写的正则匹配

/ 通用匹配, 如果没有其它匹配,任何请求都会匹配到

server {

        listen       80;

        server_name  www.toov5.com;

                     

        location =/ {     #精确匹配,注解后面不能带任何字符   匹配所有 /            有等于号 “”=“”    

                           proxy_pass http://127.0.0.1:8080;

            index  index.html index.htm;

        }

}

 传说中的API设计!

#匹配所有以/开头请求

server {
listen 80;
server_name www.toov5.com;
#匹配所有以/开头请求
location / {          #没有等于号!!!  “”=“”
proxy_pass http://127.0.0.1:8080;
index index.html index.htm;
}
}

###  以开头/toov5_8080拦截  默认开启不区分大小写     一个server两个location哦!

server {
listen 80;
server_name www.toov5.com;
### 以开头/toov5_8080 最终跳转到http://127.0.0.1:8080/;
location / toov5_8080/ {
proxy_pass http://127.0.0.1:8080/;
index index.html index.htm;
}
### 以开头/toov5_8081 最终跳转到http://127.0.0.1:8081/;
location /toov5_8081/ {
proxy_pass http://127.0.0.1:8081/;
index index.html index.htm;
}
}

目前用到的就这几个 正则表达式  后面遇到别的继续积累增加~ 未完待续~

 

 

pom

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.toov5.proxyNginx</groupId>
  <artifactId>proxyNginx</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  
  <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.0.RELEASE</version>
    </parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
         <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-freemarker</artifactId>
   </dependency>
        
    </dependencies> 
</project>

 yml

server:
  port: 8081  

 后台

package NginxTest;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class IndexController {
    @Value("${server.port}") 
    private String port;
    @RequestMapping("/")
    public String index(){
        return port;
    }
}

启动类

package NginxTest;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class app {
  public static void main(String[] args) {
    SpringApplication.run(app.class, args);
}
}

运行:

 

posted @ 2018-11-05 14:49  toov5  阅读(7689)  评论(0编辑  收藏  举报