NGINX 在 proxy_pass 中将子域重写为 URL
NGINX Rewriting subdomains as URL in a proxy_pass
我想将我域的特定子域重定向到我的后端
传递给后端的 URL 的前缀。这是因为我只有一台服务器,我不想因为增加的复杂性而不得不在后端处理多个域。
因此如果我有:
sub1.domain.com
=> domain.com/sub1/
sub1.domain.com/pathname
=> domain.com/sub1/pathname
sub1.domain.com/pathname?searchquery
=> domain.com/pathname?searchquery
等等。
到目前为止,我得出的结论如下:
server {
charset utf8;
listen 80;
server_name
domain.com,
sub1.domain.com,
sub2.domain.com,
sub3.domain.com,
sub4.domain.com,
sub5.domain.com;
# Default
if ($host ~ ^domain\.com) {
set $proxy_uri $request_uri;
}
# Rewrites
if ($host ~ (.*)\.domain\.com) {
set $proxy_uri $request_uri;
}
location / {
expires 1s;
proxy_pass http://node:8080$proxy_uri; #node is an internally listed host (docker container)
proxy_set_header Host domain.com;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_cache_valid 200 1s;
}
}
但不幸的是,我得到的只是一个 502: Bad Gateway 和以下日志,2017/06/11 12:49:18 [error] 6#6: *2 no resolver defined to resolve node, client: 136.0.0.110, server: domain.com:8888,, request: "GET /favicon.ico HTTP/1.1", host: "sub1.domain.com:8888", referrer: "http://sub1.domain.com:8888/"
知道如何实现我的目标吗?任何帮助将不胜感激:)
干杯!
看来我离答案不远了 - 在服务器块之前添加一个上游块足以将配置最终确定为所需的效果。
upstream backend {
server node:8080;
keepalive 8;
}
我还必须将代理传递行稍微修改为以下内容:
proxy_pass http://backend$proxy_uri;
这个问题很可能与 NGINX 如何解析代理传递 url 相关 - 如果阅读此内容的任何人可以深入了解原因,请编辑此答案!
我想将我域的特定子域重定向到我的后端 传递给后端的 URL 的前缀。这是因为我只有一台服务器,我不想因为增加的复杂性而不得不在后端处理多个域。
因此如果我有:
sub1.domain.com
=>domain.com/sub1/
sub1.domain.com/pathname
=>domain.com/sub1/pathname
sub1.domain.com/pathname?searchquery
=>domain.com/pathname?searchquery
等等。
到目前为止,我得出的结论如下:
server {
charset utf8;
listen 80;
server_name
domain.com,
sub1.domain.com,
sub2.domain.com,
sub3.domain.com,
sub4.domain.com,
sub5.domain.com;
# Default
if ($host ~ ^domain\.com) {
set $proxy_uri $request_uri;
}
# Rewrites
if ($host ~ (.*)\.domain\.com) {
set $proxy_uri $request_uri;
}
location / {
expires 1s;
proxy_pass http://node:8080$proxy_uri; #node is an internally listed host (docker container)
proxy_set_header Host domain.com;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_cache_valid 200 1s;
}
}
但不幸的是,我得到的只是一个 502: Bad Gateway 和以下日志,2017/06/11 12:49:18 [error] 6#6: *2 no resolver defined to resolve node, client: 136.0.0.110, server: domain.com:8888,, request: "GET /favicon.ico HTTP/1.1", host: "sub1.domain.com:8888", referrer: "http://sub1.domain.com:8888/"
知道如何实现我的目标吗?任何帮助将不胜感激:)
干杯!
看来我离答案不远了 - 在服务器块之前添加一个上游块足以将配置最终确定为所需的效果。
upstream backend {
server node:8080;
keepalive 8;
}
我还必须将代理传递行稍微修改为以下内容:
proxy_pass http://backend$proxy_uri;
这个问题很可能与 NGINX 如何解析代理传递 url 相关 - 如果阅读此内容的任何人可以深入了解原因,请编辑此答案!