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 domain.com—should be hosted here. But sub.domain.com needs to redirect to a specific URL, e.g. 12.34.56.78/a/particular/path/.
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 {
server_name domain.com;
root /var/www/domain/html;
access_log /var/log/nginx/www.demo.com.access.log;
error_log /var/log/nginx/www.demo.com.error.log;
# INCLUDE CONFIG INFO COMMON TO ALL SITES
include global/common.conf;
# INCLUDE COMMON WORDPRESS INFO
include global/wordpress.conf;
include fastcgi_params;
fastcgi_param SERVER_NAME $host;
fastcgi_param DOCUMENT_ROOT root;
}
server {
server_name sub.domain.com;
rewrite ^(.*) http://12.34.56.78/a/particular/path/ 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 ‘sub.domain.com’.
Thanks anybody for your help!
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!
Hi, add this code into subdomain server block location / { return 301 https://www.crazytut.com$request_uri; } Remember change your domain you want to redirect.
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 sub.domain.com.
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.
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.