How do I permanently redirect subdomain to specific URL?

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!

Submit an answer

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

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.

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.


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.

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