我想创建一个位置,捕获http://example.com和http://example.com/和另一个位置,捕获其他一切。第一个是静态html,另一个是api和其他东西。我试过这样做:
location ~ /?$ {
root /var/www/prod/client/www;
}
location ~ /.+ {
# https://stackoverflow.com/questions/21662940/nginx-proxy-pass-cannot-have-uri-part-in-location
rewrite ^/(.*) /$1 break;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_connect_timeout 1s;
proxy_send_timeout 30s;
proxy_read_timeout 300s;
send_timeout 300s;
proxy_redirect off;
proxy_pass http://backendPro;
}但不起作用。我在第一个位置尝试了许多选项,但当它匹配时,第一个匹配,第二个不匹配,反之亦然:
location / {
location ~ \/?$ {
location ~ ^/?$ {发布于 2018-06-14 05:34:23
它实际上要简单得多:
location = / {
# Exact domain match, with or without slash
}
location / {
# Everything except exact domain match
}因为location = /更具体,所以如果只调用域名(位置块的顺序并不重要),则始终是首选的。
在Nginx中你需要的正则表达式比你想象的要少得多,因为正常的位置块匹配每个匹配开头的网址,所以location /bla匹配以/bla开头的域中的每个网址(如/blabla,或/bla/blu,或/bla.jpg)。如果您需要捕获组并使用这些组执行某些操作,我将主要推荐regex。
https://stackoverflow.com/questions/50844472
复制相似问题