zl程序教程

您现在的位置是:首页 >  前端

当前栏目

Nginx的Location正则表达式详解程序员

2023-06-13 09:20:19 时间
location的作用

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

location的语法

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

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

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

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

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

server {

  listen  80;

  server_name  blog.ytso.com;

                     

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

    proxy_pass http://127.0.0.1:8080;

  index  index.html index.htm;

  }

}

 传说中的API设计!

#匹配所有以/开头请求

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

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

server {
listen 80;
server_name blog.ytso.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;
}
}

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

Nginx的Location正则表达式详解程序员

 

 

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); 

}

运行:

Nginx的Location正则表达式详解程序员

 

原创文章,作者:Maggie-Hunter,如若转载,请注明出处:https://blog.ytso.com/6459.html

服务器部署程序员系统优化网站设置运维