Nginx location

Nginx Liemer_Lius 655℃

参考

https://www.cnblogs.com/ronghua/p/13043466.html

语法规则: location [=|~|~*|^~] /uri/ {… }
首先匹配 =,其次匹配^~,其次是按文件中正则的先后顺序匹配,当有匹配成功时候,停止匹配并按当前匹配规则处理请求,其他正则无法匹配则最后交由/通配。

Exp1:对比=,^~,~,~*哪个优先级最高

# cat lius.conf
server {
    listen       80;
    server_name  lius.com;
    location ^~ /lius {
        default_type 'text/plain';
        return 200 "^~ /lius\n";
    }
    location ~ /lius {
        default_type 'text/plain';
        return 200 "~ /lius\n";
    }
    location ~* /lius {
        default_type 'text/plain';
        return 200 "~* /lius\n";
    }
    location = /lius {
        default_type 'text/plain';
        return 200 "= /lius\n";
    }
}
# curl lius.com/lius   # hosts劫持lius.com
= /lius

结论:说明=的优先级是最高的【必须是完全匹配】,与在配置文件中的位置无关;

Exp2:对比^~,~,~*哪个优先级最高,同为^~的匹配原则?

server {
    listen       80;
    server_name  lius.com;
    location ^~ /liu {
        default_type 'text/plain';
        return 200 "^~ /liu\n";
    }
    location ~ /liusddd {
        default_type 'text/plain';
        return 200 "~ /liusddd\n";
    }
    location ~* /liusddd {
        default_type 'text/plain';
        return 200 "~* /liusddd\n";
    }
    location ^~ /lius {
        default_type 'text/plain';
        return 200 "^~ /lius\n";
    }
}
# curl lius.com/liusddd
^~ /lius

结论:

1、说明^~的优先级仅次于=,~*,~即使更加匹配,也优先经过^~的匹配;

2、同级的^~匹配原则遵循【更精准地匹配】,liusddd匹配lius,自然匹配liu,但lius更精准地匹配【下面例子排除顺序影响】;

Exp3:同为^~,顺序是否影响?

# cat lius.conf 
server {
    listen       80;
    server_name  lius.com;

    location ^~ /liu {
        default_type 'text/plain';
        return 200 "^~ /liu\n";
    }

    location ^~ /lius {
        default_type 'text/plain';
        return 200 "^~ /lius\n";
    }
}
# curl lius.com/liusddd
^~ /lius
# curl lius.com/liu
^~ /liu
# cat lius.conf # 修改顺序
server {
    listen       80;
    server_name  lius.com;

    location ^~ /lius {
        default_type 'text/plain';
        return 200 "^~ /lius\n";
    }

    location ^~ /liu {
        default_type 'text/plain';
        return 200 "^~ /liu\n";
    }
}
# curl lius.com/liusddd
^~ /lius
# curl lius.com/liu
^~ /liu

结论:同为~^,匹配与顺序无关,只遵循【更精准地匹配】原则;

location上下文【带不带/】配置说明

参考:https://www.jb51.net/article/244331.htm

https://www.liutianfeng.com/api/upload --> localhost:8080/upload

location /api/ {
    proxy_pass http://localhost:8080/;
}

proxy_pass末尾带/, location部分就不向后端传:

URL: www.liutianfeng.com/api/upload
Proxy_Path: http://localhost:8080/
URI = /api/upload
Location = /api/
Proxy_Uri = URI - Location = upload
Definite_Proxy_Url = Proxy_Path + Proxy_Uri  = http://localhost:8080/upload

proxy_pass末尾不带/, location部分就向后端传:

URL: www.liutianfeng.com/api/upload
Proxy_Path: http://localhost:8080
URI = /api/upload
Location = /api/
Proxy_Uri = URI = /api/upload
Definite_Proxy_Url = Proxy_Path + Proxy_Uri = http://localhost:8080/api/upload

容易出问题的例子:

URL: www.liutianfeng.com/api/upload
Proxy_Path: http://localhost:8080/server/
URI = /api/upload
Location = /api
Proxy_Uri = URI - Location = /upload
Definite_Proxy_Url = Proxy_Path + Proxy_Uri = http://localhost:8080/server//upload  // 这里是多出了一个/, 一般是有问题的

转载请注明:liutianfeng.com » Nginx location

喜欢 (4)

评论已关闭。