Need to redirect from acme.com to www.acme.com with nginx? No problem … just add a virtual host declaration for the non-www that redirects like this:
|
1 2 3 4 5 6 |
server { listen 80; server_name acme.com; rewrite ^/(.*) http://www.acme.com/$1 permanent; } |
or if you want to add to an existing vhost:
|
1 2 3 |
if ( $host != 'www.domain.com' ) { rewrite ^/(.*)$ http://www.domain.com/$1 permanent; } |





3 Responses
Like .htaccess on Apache server. What kind of file will use to config?
The virtual host should be placed in the nginx.config files. Exactly which file depends on how nginx is setup on your server. For example you might have a file like /etc/nginx/sites-enabled/mydomain.conf where your primary domain is defined. This would be a good place to add the rewrite rules.
Thanks @rpenguin:disqus