Question

How do I setup a 301 redirect on Ghost, Ubuntu 18.04 DO Install

Posted January 21, 2021 334 views
NginxGhostUbuntu 18.04

I am trying to setup a 301 permanent redirect on my Ubuntu 18.04, Ghost 3.0 DigitalOcean Site.

I found some posts that mentioned i can do this with nginx by adding a few lines to the nginx.conf file.

I tried modifying etc/nginx/nginx.conf with the following server settings:

server {
    . . .
    server_name domain.com www.domain.com;

    rewrite ^/ads.txt$ https://partner.com/ads.txt permanent;
    . . .
}

But when i run nginx -t it says “server” directive not allowed. I suspect its related to the ghost installation. Based on what I read here, it seems I may need to be modifying different nginx.conf file/s. But which ones? And where?

In /var/www/ghost/system there is an nginx-root folder, but its empty. I tried adding the modified nginx.conf file there, but it does not seem to effect anything. I’m just guessing here now. Any help?

How do i setup a simple permanent 301 redirect? I could not find any step-by-step guide, only bits and pieces and i tried to puzzle them together but it didnt work.

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.

×
Submit an Answer
2 answers

Hi @mlolm,

First you need to find the proper configuration file. Go to your Nginx folder /etc/nginx/. In there you should see another folder sites-available Go inside that folder and lists it’s contents :

ls -lah /etc/nginx/sites-available

You should see a couple of files. Usually, your domain has a configuration there the same name as your domain. However, if you created this droplet from the MarketPlace it might be a little different. What you can try to do si :

grep -il domain.com /etc/nginx/sites-available*

Of course, remember to change domain.com with your actual domain. Anyway, the above command should show you the file that has your domain inside of it. You should edit that file.

As for the redirect, can you give a little bit more information on what exactly you want that redirect to be? I’ll need it so that I can provide you with the information you need to put in your Nginx configuration file.

Regards,
KFSys

  • Thank you. This is already very helpful. I found the file I believe I need to edit. It has a server {} entry and server_name contains my domain.

    What I want to do is simply redirect a file path to an external url for that file. This was requested by my monetization provider, who will be managing my ads.txt file for me. So instead of having a version on my server, they requested to redirect to their ads.txt file.

    Thus, I want to do something like this (I have anonymized the URL paths):

    rewrite ^/ads.txt$ https://partner.com/ads.txt permanent;

    So this is what the file looks like (again i anonymized my domains and IP):

    server {
        listen 80;
        listen [::]:80;
    
        server_name mydomain.com;
        root /var/www/ghost/system/nginx-root; # Used for acme.sh SSL verification (https://acme.sh)
        rewrite ^/ads.txt$ https://partner.com/v1/ads-123.txt permanent;
    
        location / {
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto $scheme;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header Host $http_host;
            proxy_pass http://myserverip;
    
        }
    
        location ~ /.well-known {
            allow all;
        }
    
        client_max_body_size 50m;
    }
    

    One thing I would add, i modified it by downloading via my ftp client, editing it locally, and then uploaded a new file (changed old to -backup). I noticed oddly the two .conf files in this directory have the icons of folders/directories in my ftp client, yet when i download them they are a file. Thought thats a bit odd, the new config file I uploaded has a normal paper icon (file). Might just be a bug with my ftp client or something.

    I changed permissions to 777 same as the other conf file, owner root.

    I ran nginx -t with the new changes and it says everything fine! Now i’m just trying to figure out how to restart nginx.

    sudo systemctl restart nginx

    Nothing happens, simply a new line starts. I am in root user inside nginx folder. I tried all the commands listed here to restart, or reload:
    https://www.cyberciti.biz/faq/nginx-restart-ubuntu-linux-command/

    • Hi @mlolm,

      Soo, about the redirect, I believe the following will work:

      rewrite ^/ads(.*) https://partner.com/ads.txt permanent;
      

      And you seem to have placed the rewrite in the proper place.

      The command :

      systemctl restart nginx
      

      will restart Nginx but it won’t return any output. You can check if Nginx is running afterward with

      systemctl status nginx
      

This command should restart the service:

sudo service nginx restart

You can then check ps output to see when the current nginx process was started:

ps -eo pid,start_time,cmd | grep nginx

Compare that to the server’s time:

date

If that’s still not working as expected, you could try checking the nginx error log for any clues:

sudo tail -F /var/log/nginx/access.log

Re: the config file icons, I wouldn’t worry about that - if the conf file was corrupted or otherwise a problem, we’d see some errors with nginx -t or when restarting.

Hope this helps and let us know if you have any more questions