How do I permanently redirect subdomain to specific URL?

Posted November 17, 2015 84.6k views

Hi, I’m new to Digital Ocean, and also to nginx.

I have a droplet hosting a couple of different sites, and most traffic for a domain—let’s call it—should be hosted here. But needs to redirect to a specific URL, e.g.

It seems like DNS only directs to servers, and so I need to create a subdomain-specific redirect.

My file in /etc/nginx/sites-available looks like this:

server {

  root /var/www/domain/html;
  access_log /var/log/nginx/;
  error_log /var/log/nginx/;

  include global/common.conf;

  include global/wordpress.conf;

  include fastcgi_params;
  fastcgi_param SERVER_NAME $host;
  fastcgi_param DOCUMENT_ROOT root;

server {
  rewrite ^(.*) permanent;

This does not seem to be working, and my browser returns an ERR_NAME_NOT_RESOLVED.

How do I redirect that subdomain traffic to that external address? Note: I want the address in the browser to still say ‘’.

Thanks anybody for your help!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Submit an Answer
2 answers

Hi, add this code into subdomain server block
location / {
return 301$request_uri;
Remember change your domain you want to redirect.

  • Thanks!

    So this looks like it’s saying “point all traffic from root of this block to a specific URI”.

    I tried this, and commented out my rewrite rule above, but it did not work. Then I also tried with including the rule, and that doesn’t work either.

    In all cases, I assume that I need to

    sudo nginx restart
    sudo nginx -t && sudo nginx reload

    each time to make sure changes are reflected on the server.

    Any other thoughts? Should this be a separate file? I saw a post on StackOverflow which suggests that subdomain server blocks should be in a separate file, but I think I read on DO that they can all be in the same file w/ no problems…

  • Thanks @lulzsecd for your help, that pointed me in the right direction. I circled around to having to process the traffic here—the third party was too slow to get back to me.

    I did add a new subdomain server block, and it seemed useful to have it come BEFORE the main domain server block.

    Also, if anyone is reading this, adding my droplet’s IP address to my computers /etc/hosts file definitely helped me to see that things were working, didn’t have to wait for pesky DNS cache to expire.

    • Hey @eyesofjeremy, I’m facing a similar issue, can you mention your fix as well?
      Was adding subdomain server block before the main server block the only issue?

      • Hi @kampta, I’m new to nginx, so it took me a while to work out details, and I’m not sure anymore.

        I can tell you that, looking at the file in sites-enabled, I see that subdomain server block coming first:

        server {
          location / {
            return 301;
        server {
          root /var/www/localdomainfolder/html;
          access_log /var/log/nginx/;
          error_log /var/log/nginx/;

        Also note that I have both the ‘www’ and non-www domains listed in the second server block…

        Hope that helps!


If the sub-domain doesn’t need to resolve to the same server as the parent domain, you really don’t need to setup a server block or redirect; it can be handled by a DNS entry. You can do this by simply adding an A entry to your DNS zone; one that points the sub-domain to the alternate IP.

It’d look something like:

A           sub             123.45.678.90

If you need support for www on the sub-domain too:

A           sub             123.45.678.90
CNAME       www.sub

This will keep the sub-domain in the browser as well. An internal redirect, such as what you have above, is just that, a redirect, which means that when X is accessed, the server block is accessed, which forces the server to processed the request, and then redirect it to Y.

With a DNS entry, you alleviate the need for the server to process anything as the DNS zone processes the request for you.