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?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With