nginx redirect www to non-www (or the other way round)
Send www.example.com to example.com (or the reverse) with one permanent redirect.
Search engines treat www.example.com and example.com as two sites unless you pick one. This config sends the other address to your chosen one with a permanent 301 redirect. Keep your main server block as it is and add this one beside it.
Create your config file
Type your own values. The file below updates as you type, and nothing you type leaves your browser.
server {
listen 80;
server_name www.example.com;
return 301 https://example.com$request_uri;
}How this setup works
Step-by-step setup
- Save the config above as /etc/nginx/conf.d/redirect-www.conf.
- Make sure your main server block uses the address you want to keep in its server_name.
- If you redirect HTTPS too, the certificate must cover both the www and non-www names.
- Test the config: sudo nginx -t.
- Apply it: sudo systemctl reload nginx.
- Check the redirect: curl -I http://www.example.com should show a 301 and a Location line.
What the important lines mean
- return 301 sends a permanent redirect, which tells search engines to move to the new address.
- $request_uri keeps the path and query, so /page?x=1 stays /page?x=1.
- server_name picks which requests this block answers, here only the address being redirected.
- The 443 block is needed because browsers on HTTPS never reach the port 80 block.
Common errors and how to fix them
- ERR_TOO_MANY_REDIRECTS
- Two redirects point at each other. Your main server block must not redirect the address you chose to keep, and the app or CMS must not also redirect it.
- Your connection is not private (certificate name mismatch)
- The certificate does not list the address being redirected. Issue one that covers both example.com and www.example.com, for example with certbot -d example.com -d www.example.com.
- nginx: [warn] conflicting server name
- Two server blocks use the same server_name on the same port. Remove or change the duplicate.
- nginx -t says "duplicate listen options"
- Only one block per port may carry options such as default_server. Remove the extra one.
Frequently asked questions
- Should I use www or non-www?
- Either is fine for search. Pick one, use it everywhere and redirect the other. Non-www is shorter. www can be easier to move to a CDN later.
- Is 301 or 302 right for this?
- Use 301. It is permanent, so search engines move to the new address and keep your ranking.
- Do I need an SSL certificate for the redirect?
- For http:// requests no. For https://www requests yes, because the browser checks the certificate before it sees the redirect.
Free tools that help
- Common ports listSearch a list of common TCP and UDP port numbers: SSH 22, HTTP 80, HTTPS 443, MySQL 3306, PostgreSQL 5432 and more, with dev server ports.
Fix a problem with this setup
- Localhost redirected too many timesFix ERR_TOO_MANY_REDIRECTS on localhost: clear cookies, fix HTTP and HTTPS rules, and WordPress URL settings.
- localhost https SSL errorFix ERR_SSL_PROTOCOL_ERROR and invalid response errors on localhost: use http, clear the saved HTTPS rule, or set up a trusted local certificate.
More server configs
- nginx load balancer configSplit traffic across two or three copies of your app with an nginx upstream block.
- nginx rate limit configSlow down abusive visitors with limit_req: a request rate per IP, a burst, and a 429 answer.
- php.ini settingsSet the PHP values most sites need: upload size, memory, run time, errors, time zone and OPcache.
- MySQL my.cnf settingsA small MySQL or MariaDB override file for connections, memory, character set and the slow query log.
- nginx reverse proxy configSend visitors from port 80 to an app running on port 3000.
- nginx PHP-FPM configRun a PHP site (WordPress, Laravel or plain PHP) on nginx with PHP-FPM.