Nginx问题整理

919 阅读1分钟

*.dubbo.dev.test.com无法解析

最多匹配三级,超出无法匹配。

proxy_pass引用变量后显示502

配置resolver为dns服务的地址。proxy_pass引用变量后改为每次请求动态解析域名,因此需要配置resolver。

变量尽量使用${}的格式,防止歧义。

没有转发location中匹配的路径

proxy_pass末尾若有"/",则不会把location中匹配的路径部分代理走;如果没有"/",则会把匹配的路径部分也给代理走。

# 如果请求是http://www.test.com/surrogates/test,则会被代理成http://surrogates.proxy/test
servername www.test.com;
location /surrogates
{
  proxy_pass http://surrogates.proxy/;
}
# 如果请求是http://www.test.com/surrogates/test,则会被代理成http://surrogates.proxy/surrogates/test
servername www.test.com;
location /surrogates
{
  proxy_pass http://surrogates.proxy;
}

proxy_pass引用变量后在转发时无法带上后面的请求参数

需要手动添加。

server
{
  resolver 10.96.0.10;
  listen 80;
  server_name *.www.aixuexi.com;
  set $environment "";
  if ($host ~* "^(.*)\.(.*)\.aixuexi\.com$")
  {
    set $environment $1;
  }
  if ($request_uri ~ /(.*?\/)(.*))
  {
    set $params $2;
  }
  location /surrogates
  {
    proxy_pass http://surrogates-$environment.default.svc.cluster.local/$params;
  }
}

语法