How to Rewrite domain in nginx [ quick tip ]

1 min read

In nginx, you can rewrite incoming requests for a naked domain ”example.com” to ”www.example.com” just by using a few lines. It’s so simple. I’ll show you how to do that properly.

server {
  server_name example.com;
  return 301 $scheme://www.example.com$request_uri;
}
server {
  server_name www.example.com;
  * other config 
}

second way

server {

listen 80;
server_name example.com ;
return  http://www.example.com$request_uri;
}

server {
listen 80;
server_name www.example.com;
* other configuration options 
}

Download or embed this code on your blog from https://paste.computersnyou.com/31.

If you’re having any kind of problem, please post your question in the comments or forum.

Nginx Wiki

Common Nginx Pitfalls



  • Home
  • About