Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rewriting wildcard domain with HTTPS

Tags:

nginx

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 ?

like image 551
Sijan Shrestha Avatar asked Aug 25 '26 16:08

Sijan Shrestha


1 Answers

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;
}
like image 163
Richard Smith Avatar answered Aug 28 '26 22:08

Richard Smith



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!