How do I do a 301 redirect via Nginx?

Hi there,

I recently set up blog.club.in using the digitalocean 1-click process.
Then I moved it over to blog.club.com using the following commands

  sudo -i -u ghost-mgr
  cd /var/www/ghost
  ghost config url {enterURL}
  ghost setup nginx
  ghost setup ssl
  ghost restart

What I’m completely failing at is how to setup a 301 redirect from the .in to the .com (i.e. the ccTLD to the gTLD)

I’m not sure which file to modify either? (is it ‘default’ or one of the conf files).
do1
db2

Thanks!!

Hi akash47,

i can recommend you Google Cloud Platform for hosting Ghost.
It took me ages to just install docker, docker-compose and so on.
In addition to that Digital Ocean doesn’t support german keyboard settings and even with installed english keyboard settings it was tough.

Nevermind.

Let’s assume that you want to start a blog using a fully supported docker environment with following docker images:

nginx-proxy,
docker-gen,
letsencrypt,
ghost (blog software) and
mariaDB.

Then you will find my answer here (including redirect from non-www to www):

Assuming your sites-enabled folder contains the blog.club.in.conf and blog.club.in-ssl.conf, you need to edit those files (sites-available is a list of configuration files you can use, and sites-enabled is (usually a linked list) of configuration files that will be used; by linked list, I mean when you edit something in sites-available, it will change in sites-enabled as well)! It would look something like this in the end:

(non-SSL)

server {
    listen 80;
    listen [::]:80;

    server_name <%= url %>;
    root <%= webroot %>;

    location / {
        # Force SSL and redirect
        return 301 https://blog.club.com$request_uri
    }

    location ~ /.well-known {
        # Allows SSL certificate renewal
        allow all;
    }
}

(SSL)

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

    server_name <%= url %>;
    root <%= webroot %>;

    ssl_certificate <%= fullchain %>;
    ssl_certificate_key <%= privkey %>;
    include <%= sslparams %>;

    location / {
        # Force SSL and redirect
        return 301 https://blog.club.com$request_uri
    }

    location ~ /.well-known {
        # Allow SSL certifcate renewal
        allow all;
    }
}

Note that <%= {var} %> are variables that you should see in your files

After you do that, run sudo nginx -t to make sure you didn’t break anything, and then run sudo nginx -s reload to refresh the configuration file.

1 Like

Thanks Vikas,

This worked perfectly fine for me.

Essentially I just kept my files exactly how they were before, and added the below line.

Thanks again

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.