Expressão regular do nome do host no proxy_pass uri no Nginx

4

Eu quero fazer algo como:

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

location / {
    proxy_pass      "http://example.org:8080/$subdomain";
}
    
por jpiasetz 13.01.2012 / 00:28

2 respostas

5

Não testado, mas dê uma chance a isso:

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

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

location / {
    proxy_pass http://example.org:8080/$subdomain;
}
    
por 13.01.2012 / 03:03
0

Testado e funcionando:

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

location / {
    echo $1;
}

Ao solicitar foo.dummy.local , ele exibirá 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
    
por 20.09.2018 / 11:17

Tags