1

I have a wildcard certificate that secures *.example.com and I need to strip out the canonical www for all requests made for subdomains, eg: www.subdomain1.example.com => subdomain1.example.com

I reviewed this question: https://stackoverflow.com/questions/11323735/nginx-remove-www-and-respond-to-both

But the first server block they suggest:

server {
  server_name www.example.com;
  return 301 $scheme://example.com$request_uri;
}

does not work for www.subdomain.example.com

How can I catch and return a scheme for www.*.example.com ?

I reviewed another question: https://serverfault.com/questions/249952/wildcard-vhosts-on-nginx in which they use regular expression to match the server name, but I'm not sure how to apply this to my situation.

Here is my current setup:

server {
    listen [::]:80 ipv6only=off;
    server_name example.com *.example.com;
    return 301 https://$host$request_uri;
}

server {

    # SSL configuration

    listen 443 ssl http2 default_server;
    listen [::]:443 ssl http2 default_server;
    include snippets/ssl-example.com.conf;
    include snippets/ssl-params.conf;
    server_name example.com *.example.com;

    root /usr/share/nginx/webroot;
    index index.php index.html index.htm;

    rewrite /wp-admin$ $scheme://$host$uri/ permanent;

    #subdomain multi site with wp in 'wp' subdir
    if (!-e $request_filename) {
    # Redirect wp-* files/folders
    rewrite ^(/[^/]+)?(/wp-.*) /wp/$2 last;

    # Redirect other php files
    rewrite ^(/[^/]+)?(/.*\.php) /wp/$2 last;
    }

    ...(etc)
}
  • hmmm is there a WP specific component you're unsure of? It might be better moving this to stackoverflow – Tom J Nowell Mar 11 at 21:15
  • You're right. I drifted away from a WP-specific issue in this case. – Elkrat Mar 13 at 2:57
0

You can use regular expressions in the server_name directive, but wildcard names (e.g. *.example.com) take precedence. See this document for details.

For example:

server {
    listen [::]:80 ipv6only=off;
    server_name ~^(www\.)?(?<name>(.+\.)?example\.com)$;
    return 301 https://$name$request_uri;
}

server {
    listen 443 ssl;
    listen [::]:443 ssl;
    include snippets/ssl-example.com.conf;
    include snippets/ssl-params.conf;

    server_name ~^www\.(?<name>(.+\.)?example\.com)$;
    return 301 https://$name$request_uri;
}

server {
    listen 443 ssl http2 default_server;
    listen [::]:443 ssl http2 default_server;
    include snippets/ssl-example.com.conf;
    include snippets/ssl-params.conf;

    ...
}

The first server block matches http requests to any subdomain, and redirects to the non-www variant using https.

The second server block matches https requests to subdomains which begin with www. and redirects to the non-www variant.

The third server block does not need a server_name directive (as it is the default server) and handles all https requests to the main domain and non-www subdomains.

  • Thank you very much. And the document was instructive as well - it says A special wildcard name in the form “.example.org” can be used to match both the exact name “example.org” and the wildcard name “*.example.org”. But I'm not sure if their illustration of *.example.org implies an infinite number of prepended, dot-separated characters or not. I may play around with this because of the rare edge case where you have https : // www.subdomain.example.com which still chokes with the regex you provided, and for which I am very grateful! :) – Elkrat Mar 13 at 2:59
  • The * represents one or more subdomain prefixes. Using a wildcard name will break all of regular expression domains it matches. – Richard Smith Mar 13 at 10:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.