How do I setup a 301 Redirect via Nginx for my Ghost blog?

Hi there,

I done the one click Ghost install and this works fine.

Then I decided to move my blog: FROM: (ccTLD) TO: (gTLD)

Basically from a ccTLD to a gTLD.

How do I setup a 301 with Nginx to redirect all http and https traffic from my ccTLD to the .com?


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.

Site Moderator
Site Moderator badge
July 31, 2023


Sure, here’s how you can set up a 301 redirect using Nginx for your case.

First, you should backup your existing Nginx configuration file:

sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/default.bak

Next, open the Nginx configuration file in a text editor. I’ll use nano for this example:

sudo nano /etc/nginx/sites-available/default

Replace the existing server block for your ccTLD with a new one that includes a return directive to handle the redirect:

server {
    listen 80;
    listen [::]:80;
    return 301 $scheme://$request_uri;

server {
    listen 443 ssl;
    listen [::]:443 ssl;

    ssl_certificate /etc/letsencrypt/live/;
    ssl_certificate_key /etc/letsencrypt/live/;
    include /etc/letsencrypt/options-ssl-nginx.conf;
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
    return 301 $scheme://$request_uri;

Replace with your ccTLD and replace with your gTLD. Also, make sure the paths to the SSL certificate and key files are correct.

This configuration listens for both HTTP (port 80) and HTTPS (port 443) requests to your ccTLD and redirects them to the equivalent URL on your gTLD using a 301 (permanent) redirect.

Save the file and exit the text editor.

Check your Nginx configuration for syntax errors:

sudo nginx -t
sudo service nginx restart

Now, any traffic to your ccTLD should be redirected to your gTLD.

Keep in mind that it’s recommended to backup your Nginx configuration file before making changes, and always check the configuration syntax before reloading Nginx.

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel