我试图用Nginx反向代理api。我有以下配置:
worker_processes 4;
events { worker_connections 1024; }
http {
upstream some_upstream {
server 1.something.com;
server 2.something.com;
}
server {
listen 80;
location ~/proxyNow/(?<zvar>(\w+))/(?<xvar>(\w+))/(?<yvar>(\w+))/ {
proxy_pass http://some_upstream/hello/something/$zvar/$xvar/$yvar/somethingelese;
proxy_set_header Host $http_host;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_cache maps_cache;
proxy_cache_valid 200 302 365d;
proxy_cache_valid 404 1m;
proxy_redirect off;
}
}
}
当我尝试拨打以下网址http://localhost:82/proxyNow/1/2/3/?app_id=someAppId&app_code=someCode
我收到以下错误消息:
网址无效
请求的网址 &#34; http://%5bNo%20Host%5d/hello/something/1/2/3/somethingelese&#34;,是 无效。参考#9.be35dd58.1489086561.5c9bd3c
似乎nginx无法检索主机。但是如果我直接执行调用:
http://1.something.com/hello/something/$zvar/$xvar/$yvar/somethingelese?app_id=someAppId&app_code=someCode
http://2.something.com/hello/something/$zvar/$xvar/$yvar/somethingelese?app_id=someAppId&app_code=someCode
由于某种原因,Nginx似乎无法解析主机
答案 0 :(得分:0)
您应该查看文档。
http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_pass
使用正则表达式指定位置时。
在这种情况下,应该指定指令而不使用URI。
我建议使用以下解决方案而不需要昂贵的正则表达式位置 http://nginx.org/en/docs/http/ngx_http_core_module.html#location
location /proxyNow/ {
rewrite /proxyNow/(?<zvar>(\w+))/(?<xvar>(\w+))/(?<yvar>(\w+))/.* /hello/something/$zvar/$xvar/$yvar/somethingelese$is_args?$args break;
proxy_pass http://some_upstream;
... other nginx diretives;
}