Answer the question
In order to leave comments, you need to log in
How to compose subdomain rewrite with regex on nginx?
server_name ~^(?<name>.+)\.test\.server$ test.server;
rewrite ^(.*)$ http://test.server/$name permanent;
root /usr/share/nginx/html;
location / {
}
}
server{
listen 80;
server_name domain.test.server test.server;
root /usr/share/nginx/html;
location / {
if ($host ~* ^(?<sub>.+)*\.test\.server$ )
{ rewrite ^/(.*)$ http://test.server/$sub/$1; }
}
}
}
curl -I http://domain.test.server/
HTTP/1.1 302 Moved Temporarily
Server: nginx/1.12.2
Date: Fri, 20 Sep 2019 10:17:52 GMT
Content-Type: text/html
Content-Length: 161
Connection: keep-alive
Location: http://test.server/domain/
Answer the question
In order to leave comments, you need to log in
server{
listen 80;
server_name domain.test.server test.server;
root /var/www/korin;
location / {
proxy_set_header Host test.server;
proxy_pass http://127.0.0.1:80;
# if ($host ~* ^(?<lev3>.+)*\.(?<lev2>.+)*\.(?<lev1>.+)*$ ) {
# rewrite ^/(.*)$ /$lev3/$lev2/$lev1/$1 break;
if ($host ~* ^(?<sub>.+)*\.test\.server$ ) {
rewrite ^/(.*)$ /$sub/$1 break;
}
}
}
server{
listen 127.0.0.1:80;
server_name test.server;
root /var/www/korin;
location / {
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question