V
V
VITYA-XY12019-09-19 19:58:45
Nginx
VITYA-XY1, 2019-09-19 19:58:45

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 / {
          }

       }

Rewrite is needed, redirect is not suitable
I.e .: Before
: subdomain.test.server/ping
Now : test.server/subdomain/ping
UPD:
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; }
           }
   }

}

It works like this but with a redirect:
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/

How to remove a redirect?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
VITYA-XY1, 2019-09-20
@VITYA-XY1

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 question

Ask a Question

731 491 924 answers to any question