How to remove "www" and redirect to "http" to "https" on Nginx webserver
The best way to accomplish this is using three server blocks as follow:
- Redirect default http to https
- Redirect https www to https non-www
- Actual handle requests https
The reason for using extra server blocks instead of "if" directive is that server selection is performed using a hash table, and is very fast. Using a server-level "if" directive means, its run for every request, which is wasteful. Also, capturing the requested uri in the rewrite is wasteful, as Nginx already has this information in the $uri and $request_uri variables (without and with query string, respectively).