nginx redirect to domain when user enters IP address

Solution 1:

You need to add a valid https block for the IP address.

However, since you cannot get certificates which can be used with an IP address, you will be left with a URL that gives a SSL error from the browser.

Solution 2:

It's because HTTPS uses port 443. So just add another listen statement like this:

server {
    listen       80;
    listen       443;
    server_name  IP_OF_MY_VPS;

    return       301 https://example.com$request_uri;
}