I have try to config nginx: /etc/nginx/nginx.conf

server {
    listen 80 default_server;
    listen [::]:80 default_server;
    server_name; #This is my private IP
    return 301 https://$server_name$request_uri;
    # OR return 301 https://$host$request_uri

But it cannot redirect from HTTP to HTTPS. Please help me on this. Thanks.

  • 1
    Try: return 301 https://$host$request_uri; – Richard Smith Dec 5 '18 at 10:01
  • I have already tried this but the direction still not working. – Brian Dec 5 '18 at 11:03
  • Ok, $server_name will not work as it will use an IP address and the secure server will not like that. Have you tried: curl -I http://example.com? – Richard Smith Dec 5 '18 at 11:17

I think the issue is the IP as server name. Try and edit your hosts file and add an entry for the IP. For example: myserver.test

The edit the nginx configuration and change the server_name parameter to:

server_name myserver.test;

restart nginx or reload the configuration.

  • HTTPS needs to use SSL with HTTP protocol to build a secure and verified connection. It is heavily relies on the domain name. In case you are specifing the IP, it won't work as expected. You can use ` server_name _;` as your server name, but your client must have a valid DNS configuration, at least hosts file needs to know to which IP redirect your request. – Dmitriy Kupch Dec 5 '18 at 16:44

Your Answer

By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.