Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redirect all but one subdomains?

I've got a domain example.com and would like to redirect all subdomains to http://example.com . One exception however would be the backend-subdomain which is api.example.com.

How do I pull this off using Nginx?

like image 262
Hedge Avatar asked Oct 27 '25 10:10

Hedge


1 Answers

You can use combination of multiple servers (including the one with wildcard subdomain). Here is a minimal example of such config:

server {
    listen          80;
    server_name     api.example.com;
    add_header      Content-Type    text/plain;
    return  200     "api";
}
server {
    listen          80;
    server_name     *.example.com;
    return  301     $scheme://example.com$request_uri;
}
server {
    listen          80;
    server_name     example.com;
    add_header      Content-Type    text/plain;
    return  200     "main";
}

You can read more about configuring server names in the docs: http://nginx.org/en/docs/http/server_names.html

like image 194
Oleg Avatar answered Oct 30 '25 12:10

Oleg



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!