This is the one that got me going:
https://stackoverflow.com/a/3766957/447516
if ($host ~* ^[^.]+\.[^.]+$) {
rewrite ^(.*)$ http://www.$host$1 permanent;
}
Question
How can I redirect non-www to www in NGINX when the domain name is not predetermined? All the examples I've found show domain.com or example.com. I need to redirect for any domain. Any pointers?
Solution 2
This is the one that got me going:
https://stackoverflow.com/a/3766957/447516
if ($host ~* ^[^.]+\.[^.]+$) {
rewrite ^(.*)$ http://www.$host$1 permanent;
}
OTHER TIPS
You can specify the default server to catch domains that are not specified in the server_name
directives of your configuration.
listen 80 default_server;
will make that server catch non matching domain names (assuming the port is 80 on all interfaces).
This is a clear explanation of this behavior.
(not that before nginx 0.8.21 default
(not default_server
) has to be specified).