Nginx $upstream_addr variable doesn't work in if condition

岁酱吖の 提交于 2020-02-25 06:30:34

问题


I'm running a reverse proxy using proxy_pass directive from ngx_http_proxy_module. I want to forbid access to certain backend IP address ranges (like 172.0.0.0/24). I've tried

if ($upstream_addr ~* "^172.*") {                                     
    return 403;                                                    
}
add_header X-mine "$upstream_addr";

both in server and location context but it doesn't work, i.e. Nginx still returns 200:

$ curl localhost -I
HTTP/1.1 200 OK
Server: nginx/1.17.0
Date: Thu, 13 Feb 2020 12:58:36 GMT
Content-Type: text/html
Content-Length: 612
Connection: keep-alive
Last-Modified: Tue, 24 Sep 2019 14:49:10 GMT
ETag: "5d8a2ce6-264"
Accept-Ranges: bytes
X-mine: 172.20.0.2:80

What am I missing? (Note that I added the content of $upstream_addr variable to X-mine header for debugging.)

来源:https://stackoverflow.com/questions/60208343/nginx-upstream-addr-variable-doesnt-work-in-if-condition

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!