Question

How to Redirect Droplet IP Access to Domain URL

Hello everyone,

I followed DO’s tutorial (https://www.digitalocean.com/community/tutorials/how-to-install-wordpress-with-lemp-on-ubuntu-18-04) along with all the pre-requisites which also include securing Nginx with Certbot.

My domain is properly being redirected from HTTP-to-HTTPS. No problem at that end.

Now, I have a few questions:

I can’t access my website through my Droplet IP (it serves 404). If I add HTTPS to the droplet IP, it shows the following and if I proceed, I can access the website.

First question: Why is this happening?

Second question: I want to redirect my droplet’s IP access to my actual domain URL (both when using HTTP and HTTPS). How can I do this?

Following is my default config file:

server {
        root /var/www/html;
        index index.php index.html index.htm index.nginx-debian.html;
        server_name example.com www.example.com;
        client_max_body_size 0;

        location / {
                #try_files $uri $uri/ =404;
                try_files $uri $uri/ /index.php$is_args$args;
        }

        location ~ \.php$ {
                include snippets/fastcgi-php.conf;
                fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
        }

        location ~ /\.ht {
                deny all;
        }

        location = /favicon.ico { log_not_found off; access_log off; }
        location = /robots.txt { log_not_found off; access_log off; allow all; }
        location ~* \.(css|gif|ico|jpeg|jpg|js|png)$ {
           expires max;
           log_not_found off;
        }

        location = /xmlrpc.php { deny all; }

    listen 443 ssl http2; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

}

# Expires map
map $sent_http_content_type $expires {
    default                    off;
    text/html                  epoch;
    text/css                   max;
    application/javascript     max;
    ~image/                    max;
}

server {
    if ($host = www.example.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    if ($host = example.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


        listen 80;
        server_name example.com www.example.com;
    return 404; # managed by Certbot

    expires $expires;
}

It’d be great if someone could help me, I tried looking up and did find some solutions by creating a new server block, but that didn’t fit my current defined server blocks.

Best regards, Dhananjay Bhardwaj

Subscribe
Share

Submit an answer
You can type!ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

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.

Hi @dhananjaygbhardwaj,

So, this is happening as you don’t have an SSL to your IP address, which is normal. However, when you try to enter via HTTPS, Nginx tries to find an SSL and the first SSL it finds is your website’s, thus showing your website.

To stop this, you can create another listen directive for your default configuration file on port 443 which redirects to a 404 error like you’ve done for your port 80

        listen 80;
        server_name example.com www.example.com;
    return 404; # managed by Certbot

Regards, KFSys

Hi @dhananjaygbhardwaj,

regarding the last comment. I’ll recommend doing an actual 301 redirection to the website itself rather than just leaving it to open the website’s information. That way you won’t have the SSL error you are describing.