server { listen 80; listen [::]:80; server_name "~^www.(?.*localhost)$"; # www redirect to non-www return 301 http://$domain$request_uri; }