Question

Nginx https to http

I have an issue with a freshly configured Nginx setup on Ubuntu 16.04. My site loads fine using http, but I get a connection refused error when I access it using https. I don’t need https at the moment, so I would like to redirect these request to http.

My server block looks almost exactly like the LEMP tutorial (https://www.digitalocean.com/community/tutorials/how-to-install-linux-nginx-mysql-php-lemp-stack-in-ubuntu-16-04) with the addition of a condition that checks for https, then redirects to http. For some reason, this is not working for me. Any help would be appreciated!

See my edited down server block:

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

        server_name domain.com www.domain.com;

        if ( $https = "on" ) {
                return 301 http://$host$request_uri;
        }
}

Submit an answer

This textbox defaults to using Markdown to format your answer.

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

Sign In or Sign Up to Answer

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.

Accepted Answer

@pixelment @jtittle You should setup Lets Encrypt - and keep your site on HTTPS-only (with a redirect from HTTP). There’s multiple advantages to HTTPS - first is that it allows http/2, which is version 2 of the HTTP protocol. This will speed up your site a lot, since all files on the site’s connection is streamed through the same tunnel. And search engines are actually preferring HTTPS, so you’ll get a lower ranking if you don’t have it.

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.

This comment has been deleted

@pixelment

With the server block provided, SSL isn’t configured, thus will not work. If you’re not listening on port 443, then SSL/HTTPS is not active, thus requests for it will result in an error.

Instead of trying to trying to redirect HTTPS => HTTP, I would simply recommend setting up SSL and not worrying about redirects that are typically not standard and may cause issues.

Do you have a certificate or are you using Lets Encrypt? Can you post all the configuration - it’s okay if you change your domain name to domain.com