Sijan Shrestha
Sijan Shrestha

Reputation: 2266

Rewriting wildcard domain with HTTPS

Is it possible to rewrite HTTPS wildcard Domains in nginx or should we create multiple structure /file for each domain?

Lets say i have follwing:

1. subdomain1.domain.com
2. subdomain2.domain.com

If i do not have HTTPS i used the following which works great:

server {
 listen 443;
server_name *.domain.com;
charset utf-8;
}

No if i use HTTPS, i would have to write a new block like the following ( using letsencryt)

The following is just a test domain (only 1 domain )

 server {
    server_name test.me;
    rewrite ^ https://test.me$request_uri? permanent;
}

server {
    listen 443;
    server_name test.me;
    charset utf-8;
    ...
 }

Is it possible to do the same for multiple domains? server { server_name .domain.com; rewrite ^ https://.domain.com$request_uri? permanent; }

server {
    listen 443;
    server_name *.domain.com;
    charset utf-8;
    ...
 }

I tried the above config but it doesnot work, it redirects me to

https://%2A.domain.com.domain.com/  (just for test)

Is it possible to do something like this ? or Should i have different block for every subdomain ?

Upvotes: 3

Views: 2566

Answers (1)

Richard Smith
Richard Smith

Reputation: 49742

Use one of the variables provided by nginx to extract the host name from the request line. For example $host (see this document for details):

server { 
    server_name .domain.com;
    return 301 https://$host$request_uri;
}

Upvotes: 4

Related Questions