I've got two options (return and rewrite) for redirection and I'm not sure which is recommended for performance, SEO, etc:
## Redirect from non-www to www
server {
server_name example.com;
# Option 1
return 301 $scheme://$host$request_uri;
# Option 2
rewrite ^ http://$host$request_uri? permanent;
}
## Default server config
server {
...
listen 192.168.1.1:80 default_server;
root /www;
server_name www.example.com;
Option 2 seems to work with curl but it's not redirecting when called by a browser and the return code is that of a temporary redirect despite the rewrite directive being set to permanent
:
curl -I example.com
HTTP/1.1 302 Moved Temporarily
Server: nginx
...
Location: http://www.example.com/
...