您的位置:首页 > 运维架构 > Nginx

Nginx路由匹配规则及各种场景实例

2020-07-14 05:42 495 查看

一、在nginx配置文件中通过location配置路由转发规则,配置语法为:

location [=||*|^~] /uri/ {
# …
}

中括号中为路由匹配符号,常见的有:

1  =:精确匹配
2  ^~:精确前缀匹配
3  ~:区分大小写的正则匹配
4  ~*:不区分大小写的正则匹配
5  /uri:普通前缀匹配
6  /:通用匹配
精确匹配

精确匹配使用 = 表示,nginx进行路由匹配的时候,精确匹配具有最高的优先级,请求一旦精确匹配成功nginx会停止搜索其他到匹配项
配置实例
location = /test {

}

精确前缀匹配

精确前缀匹配的优先级仅次于精确匹配,nginx对一个请求精确前缀匹配成功后,停止继续搜索其他到匹配项
配置实例
location ^~ /test_a {

}

正则匹配

正则匹配分为区分大小写和不区分大小写两种,分别用 ~ 和 ~* 表示;一个请求精确匹配和精确前缀匹配都失败后,如果配置有相关的正则匹配location,nginx会尝试对该请求进行正则匹配。需要说明的是正则匹配之间没有优先级一说,而是按照在配置文件中出现的顺序进行匹配,一旦匹配上一个,就会停止向下继续搜索
配置实例
# 配置1
location ~ /test_a {

}

# 配置2
location ~* /test_A {

}

对于请求 /test_a/hello 来说会被匹配到 配置1 处理,/test_A/hello 则会被匹配给 配置2 处理

普通前缀匹配

普通前缀匹配前面没有任何修饰符,直接在location后写需要匹配的uri,它的优先级次于正则匹配
配置实例
location /img {

}

通用匹配

通用匹配使用一个 / 表示,可以匹配所有请求,一般nginx配置文件最后都会有一个通用匹配规则,当其他匹配规则均失效时,请求会被路由给通用匹配规则处理;如果没有配置通用匹配,并且其他所有匹配规则均失效时,nginx会返回 404 错误
配置实例
location / {

}

引用一个完整例子

nginx路由规则配置:
location = / {
echo “规则A”;
}
location = /login {
echo “规则B”;
}
location ^~ /static/ {
echo “规则C”;
}
location ^~ /static/files {
echo “规则X”;
}
location ~ .(gif|jpg|png|js|css)$ {
echo “规则D”;
}
location ~* .png$ {
echo “规则E”;
}
location /img {
echo “规则Y”;
}
location / {
echo “规则F”;
}

请求uri 匹配路由规则
http://localhost/ 规则A
http://localhost/login 规则B
http://localhost/register 规则F
http://localhost/static/a.html 规则C
http://localhost/static/files/a.txt 规则X
http://localhost/a.png 规则D
http://localhost/a.PNG 规则E
http://localhost/img/a.gif 规则D
http://localhost/img/a.tiff 规则Y

以上是对nginx路由匹配规则的粗略总结和归纳。

二、各种场景实例:

# 1、反向代理静态文件
worker_processes  1;

events {
worker_connections  1024;
}

http {
include       mime.types;
default_type  application/octet-stream;
sendfile        on;
keepalive_timeout  65;

server {
listen       8080;
server_name  127.0.0.1;

# charset koi8-r;
location / {
root   html;
index  index.html index.htm;
}
# 配置反向代理
location /drink {
alias D:\apk\drink;
autoindex on;
allow all;
autoindex_exact_size on;
autoindex_localtime on;
}

# error_page  404              /404.html;
# redirect server error pages to the static page /50x.html
error_page   500 502 503 504  /50x.html;
location = /50x.html {
root   html;
}
}
}
# 2、配置ssl域名+反向代理静态文件+代理服务
worker_processes  1;

events {
worker_connections  1024;
}

http {
include       mime.types;
default_type  application/octet-stream;
sendfile        on;
keepalive_timeout  65;

server {
listen       8055;
listen       14430 ssl;
# 配置ssl域名 start
server_name  ****.nxycsw.cn;

ssl_certificate      D:/workspace/human_drink_yanqing/nginx-1.16.1/cert/ssl.pem;
ssl_certificate_key  D:/workspace/human_drink_yanqing/nginx-1.16.1/cert/ssl.key;

ssl_session_cache    shared:SSL:1m;
ssl_session_timeout  5m;

ssl_ciphers  HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers  on;
# 配置ssl域名 end

# 配置反向代理 start
location / {
root   D:/workspace/human_drink_yanqing/nginx-1.16.1/html/docs/dist;
index  index.html index.htm after.html front.html mobile.html;
}
# 配置反向代理 end

error_page   500 502 503 504  /50x.html;
location = /50x.html {
root   html;
}

# 配置服务代理 start
location /api {
# rewrite  ^.+api/?(.*)$ /$1 break;
include  uwsgi_params;
proxy_pass   http://127.0.0.1:8056/api; #此处修改为自己的请求地址
allow all;
autoindex_exact_size on;
autoindex_localtime on;
proxy_buffering off;
}
# 配置服务代理 end
}
}
# 3、配置负载
worker_processes 2;

events {
worker_connections  1024;
}

stream {
# 负载地址
upstream api {
server 127.0.0.1:19001 weight=10;
server 127.0.0.1:19002 weight=10;
server 127.0.0.1:19003 weight=10;
server 127.0.0.1:19004 weight=10;
}

# 朝外提供服务的端口
server {
listen 8084;  # 监听端口
proxy_connect_timeout 300s;
proxy_timeout 700s;
proxy_pass api;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: