Creating a simple url redirect service in under 10 minutes using nginx
You may ask yourself, why on earth would I want to build a simple URL redirect service... Let me explain ...
Why
This website is hosted on the DigitalOcean App Platform. You need to configure CNAME
DNS records pointing to the domain provided by the DigitalOcean App Platform to use your own custom domain like botzenhart.io
. For me the target domain is botzenhart-io-i7shs.ondigitalocean.app
.
I want to serve this website with https://www.botzenhart.io
and https://botzenhart.io
. I also want to redirect from older domains where I have the same problem as with the primary domain. This gives me some headaches when dealing with https://botzenhart.io
. Most DNS providers won't let you create CNAME
records for the top level domain, while A
records are fine.
Therefore, you need some additional service that you can use for an A
record.
In this article we will go through the tasks of setting up a simple URL redirect service that is easy to run and maintain.
TL;DR
Create a simple URL redirect service that runs on a DigitalOcean droplet. Note that this will work with any virtual machine that has a dedicated IPV4 or IPV6 address. We'll use nginx with very simple and easy to set up redirect rules, then create DNS A records pointing to the droplet/virtual machine. The whole process takes about 10 minutes to get up and running.
Stefan, more details please
First, we need to create a droplet on DigitalOcean or a virtual machine on another provider of your choice, such as Hetzner Cloud.
The DigitalOcean droplet can be created by clicking through the user interface. It's also possible to use the command line tool doctl
or good old cURL.
Create droplet using doctl
doctl compute droplet create \
--image ubuntu-22-04-x64 \
--size s-1vcpu-512mb-10gb \
--region fra1 \
static-redirecter-01
Create droplet using cURL
curl -X POST -H 'Content-Type: application/json' \
-H 'Authorization: Bearer '$TOKEN'' \
-d '{"name":"static-redirecter-01",
"size":"s-1vcpu-512mb-10gb",
"region":"fra1",
"image":"ubuntu-22-04-x64"}' \
"https://api.digitalocean.com/v2/droplets"
Install nginx
Installing nginx is simple. After connecting to the droplet via SSH, run the following commands.
apt update
apt install nginx -y
Querying for the Droplets IP address should display the default "Welcome to nginx" page.
Now we can set up the required nginx sites and define the redirect rules.
Configure nginx to redirect incoming requests
First, remove the default nginx configuration. Do this by removing the symlink in the sites-enabled
directory.
rm /etc/nginx/sites-enabled/default
This is just a symlink. It will be removed. The original file is in /etc/nginx/sites-available/default
. Nginx has a convention for having available and enabled site configurations. Available site configurations are stored in /etc/nginx/sites-available
. By creating a symlink to /etc/nginx/sites-enabled
the linked site will be enabled after nginx is reloaded or restarted.
To add a new site configuration, create a new file in /etc/nginx/sites-available
.
# /etc/nginx/sites-available/botzenhart-io-redirects.conf
server {
server_name _;
return 301 https://www.botzenhart.io;
}
This configuration redirects all incoming requests to https://www.botzenhart.io
. This can be done using the _
as for the server_name
.
If you want to manage multiple sources and destinations, you can specify a list of domains with the server_name
directive. See the official nginx documentation for more information.
I recommend naming the configuration files properly. It makes it easier to understand what you're doing. This is for your future-self.
Activate the site configuration. Create a symlink to
sudo ln -s /etc/nginx/sites-available/botzenhart-io-redirects.conf /etc/nginx/sites-enabled/botzenhart-io-redirects.conf
Validating the nginx configuration
Before you reload nginx to enable the new site redirect, make sure that your nginx configuration is valid. Do this by running
nginx -t
This should return something like the following, indicating that all is well with the configuration.
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
Activating the new site
Activate the new site by reloading nginx.
systemctl reload nginx
Testing the site configuration
You can easily test your redirections with cURL before updating your DNS settings for the domains you want to redirect.
curl --header "host:botzenhart.io" --location http://165.232.119.79 --head
The command sends a request to your VM using its IPV4 address and a host header. You'll get output like this
HTTP/1.1 301 Moved Permanently
Server: nginx/1.18.0 (Ubuntu)
Date: Wed, 15 Mar 2023 09:36:04 GMT
Content-Type: text/html
Content-Length: 178
Connection: keep-alive
Location: https://www.botzenhart.io
[...]
The important part is Location: https://www.botzenhart.io
, which indicates that you're getting the redirect you expect.
Well, I hope you have enjoyed reading this article!
I will see you next time! 👋