Redirecting Traffic from to

Posted April 1, 2019 1.1k views
NginxGhostLet's EncryptUbuntu 18.04

I’ve decided to use the one-click Ghost install on digitalocean, and I like how easy it has made the process. However, I am having an issue with my site. I want to redirect the to, but my config doesn’t seem to work.

I have Letsencrypt certs for my domain which is, and it works fine. It gives a cert error if you try, so since I don’t want people browsing to www, I’d rather just have it forwarded to I’ve setup www as a CNAME that points to the main domain.

My NGINX config is as follows (/etc/nginx/sites-available/

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

    root /var/www/ghost/system/nginx-root;

    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;


    location ~ /.well-known {
        allow all;

    client_max_body_size 50m;

I’ve tried modifying it so it looks like the following but it doesn’t redirect:

redirect 301$request_uri;

I haven’t modified

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
1 answer

Because you want to redirect a request from to you want to create a new server directive that is listening for connections on just and then add the redirect.

So you would need to update your configuration with the following:

server {
    return 301 $scheme://$request_uri;

In the snippet that you posted you had both server_names present, which may have been causing the issue.

You can find a more detailed write here:
How to redirect with Nginx

by Mitchell Anicas
This tutorial will show you how to redirect a www URL to non-www, e.g. `` to ``, with Nginx on CentOS 7. We will also show you how to redirect in the other direction, from a non-www URL to www.