0

I have several domains configured for my websites on my VPS.

What I want to achieve is: If a user enters the IP address of my VPS directly on his/her browser, like this: https://X.X.X.X, I want it to redirect to one of my websites.

I was able to make it work for HTTP (http://X.X.X.X) like this:

server {
    listen       80;
    server_name  IP_OF_MY_VPS;

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

However, if the user enters https://X.X.X.X, it doesn't work. Can someone please help out ?

2 Answers 2

2

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.

3
  • 2
    stackoverflow.com/q/2043617 and for example ssl.com/faqs/order-ssl-tls-certificate-for-ip-address seem to indicate that it really depends on the CA if they will issue a certificate for an IP-address but there isn't anything technical that actually prevents them from issuing one.
    – Bob
    Dec 21, 2020 at 7:58
  • Is there any other way then to redirect the user to my website when they enter the ip address directly in to the browser ? Because my website already has SSL to it. I just need to redirect user when they try to enter the IP address directly in the browser Dec 21, 2020 at 9:17
  • You need to have a certificate that lists the IP address in Subject Alternate Name field, along with your website's domain name. Your current certificate likely contains the domain name only. Dec 22, 2020 at 19:02
1

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;
}
9
  • It doesn't work. It gives me an error when i perform nginx -t : nginx: [emerg] no "ssl_certificate" is defined for the "listen ... ssl" directive in /etc/nginx/sites-enabled/default:1 nginx: configuration file /etc/nginx/nginx.conf test failed. I don't want to setup ssl again since my website already has ssl configured. I just want to redirecto the website. Dec 20, 2020 at 19:13
  • Sorry I didn't think so much in advance, try adding ssl off; Dec 20, 2020 at 19:28
  • Still doesn't work. Dec 20, 2020 at 19:33
  • Interesting, just tried it out and it works for me. Dec 20, 2020 at 19:36
  • I got the deprecated warning and an error statement. Can you provide the whole conf file please ? Dec 20, 2020 at 19:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .