当端点进行重定向时,Nginx变量替换看起来很奇怪(302)

时间:2016-12-07 11:32:59

标签: nginx

我有一个应该代理呼叫的nginx。

server {
  listen 80;

  root /srv/nginx;

  server_name ~(.*).smec.xx;

  location / {
      resolver 127.0.0.11;

      proxy_pass          http://$1-service:8080;
      proxy_set_header    X-Forwarded-For $remote_addr;
      proxy_set_header    Host $server_name:$server_port;
  }
}

调用例如hrman.smec.xx应代理hrman-services:8080。 输入网址http://hrman.smec.xx/login.html时,此方法正常 但是,当我只添加http://hrman.smec.xx curl -v hrman.smec.xx的输出时,它就不会出现:

* Rebuilt URL to: hrman.smec.xx/
*   Trying 172.29.200.52...
* Connected to hrman.smec.xx (172.29.200.52) port 80 (#0)
> GET / HTTP/1.1
> Host: hrman.smec.xx
> User-Agent: curl/7.49.1
> Accept: */*
>
< HTTP/1.1 302 Found
< Server: nginx/1.11.6
< Date: Wed, 07 Dec 2016 11:25:06 GMT
< Content-Length: 0
< Connection: keep-alive
< X-Content-Type-Options: nosniff
< X-XSS-Protection: 1; mode=block
< Cache-Control: no-cache, no-store, max-age=0, must-revalidate
< Pragma: no-cache
< Expires: Thu, 01 Jan 1970 00:00:00 GMT
< X-Frame-Options: DENY
< Set-Cookie: JSESSIONID=1wyuuyrw36yti4tqmi8jmn095;Path=/
< Location: http://~(.*).smec.xx/login.html
<
* Connection #0 to host hrman.smec.xx left intact

< Location: http://~(.*).smec.xx/login.html似乎错了,不是吗?我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

正如@IvanTsirulev指出的那样,解决方案是

server {
  listen 80;

  root /srv/nginx;

  server_name ~(.*).smec.xx;

  location / {
      resolver 127.0.0.11;

      proxy_pass          http://$1-service:8080;
      proxy_set_header    X-Forwarded-For $remote_addr;
      proxy_set_header    Host $host:$server_port;
  }
}