我想做类似的事情:
server_name ~^(www\.)?(?<subdomain>[^.]+).example.org$;
location / {
proxy_pass "http://example.org:8080/$subdomain";
}
答案1
未经测试,但可以尝试一下:
server_name ~^(www\.)?[^.]+.example.org$;
if ($host ~* ^(www\.)?([^.]+).example.org$) {
set $subdomain $2;
}
location / {
proxy_pass http://example.org:8080/$subdomain;
}
答案2
已测试并正常运行:
server_name "~(.*).dummy.local";
location / {
echo $1;
}
请求的时候foo.dummy.local
会显示foo
。
curl -vvv foo.dummy.local
* Rebuilt URL to: foo.dummy.local/
* Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to foo.dummy.local (127.0.0.1) port 80 (#0)
> GET / HTTP/1.1
> Host: foo.dummy.local
> User-Agent: curl/7.52.1
> Accept: */*
>
< HTTP/1.1 200 OK
< Server: nginx/1.10.3
< Date: Thu, 20 Sep 2018 09:15:54 GMT
< Content-Type: application/octet-stream
< Transfer-Encoding: chunked
< Connection: keep-alive
<
foo
* Curl_http_done: called premature == 0
* Connection #0 to host foo.dummy.local left intact