How to forward domain with Forge (nginx)

I have one quick Nginx advice for you. You can use this whether you’re on Forge, or have an Nginx server running.

If you want to set up a redirect or forward a domain on Forge, it’s as simple as adding one line of code to the server block.

But let’s take it from the top.
First, add the domain you want to forward to your server on Forge. Next, make sure the domains A-record is pointing to the server’s IP address.

Finally, open the Nginx file. In Forge, click “Edit Files” and choose “Edit Nginx Configuration”. Locate the server block, often in the top few lines. You’ll see it has a server_name attribute of your domain. Under this, you add the following line:

return 301 https://yourdomain.com$request_uri;

This will ensure that your domain will now forward to the domain you’ve added on this line. By using the 301 redirect, we use the redirect code for “moved permanently”. You can see a full list of redirect status codes here, which you can use.

Give Forge half a minute to restart Nginx. Visit the forwarded domain, and it will redirect you to the correct domain.