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); } }
運行: