In Nginx, you just need to set up something similar to this:
server {
listen 80;
server_name *.domain.dev;
...
}
Notice the:
server_name *.domain.dev;
Within your application, you will need to split/handle the HOST header.
Another approach but this implies a redirect 301 is to do something like this:
server {
listen 80;
server_name ~^(?<subdomain>\w+)\.your-domain\.tld$;
return 301 https://domain.dev/profile?user=$subdomain;
}
In this case, notice the regex in the server_name:
server_name ~^(?<subdomain>\w+)\.your-domain\.tld$;
This helps you to use the subdomain as $subdomain
.
To avoid a redirect this may work:
server {
listen 80;
server_name ~^(?<subdomain>\w+)\.your-domain\.tld$;
location / {
resolver 8.8.8.8;
proxy_pass http://httpbin.org/get?user=$subdomain;
proxy_set_header Host httpbin.org;
}
}
For testing purposes, I am using http://httpbin.org/on the
proxy_pass`, so that you could test with something like:
$ curl foo.your-domain.tld:8080
{
"args": {
"user": "foo"
},
"headers": {
"Accept": "*/*",
"Connection": "close",
"Host": "httpbin.org",
"User-Agent": "curl/7.54.0"
},
"origin": "91.65.17.142",
"url": "http://httpbin.org/get?user=foo"
}
Notice the response:
"url": "http://httpbin.org/get?user=foo"
Matching the subdomain foo
.your-domain.tld in this case.