5

I want to do something like:

server_name   ~^(www\.)?(?<subdomain>[^.]+).example.org$;

location / {
    proxy_pass      "http://example.org:8080/$subdomain";
}

2 Answers 2

7

Untested, but give this a shot:

server_name   ~^(www\.)?[^.]+.example.org$;

if ($host ~* ^(www\.)?([^.]+).example.org$) {
    set $subdomain $2;
}

location / {
    proxy_pass http://example.org:8080/$subdomain;
}
1

Tested and working:

server_name "~(.*).dummy.local";

location / {
    echo $1;
}

When requesting foo.dummy.local, it will display 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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .