Plain HTTP Request Was Sent to HTTPS Port'

How to Fix ‘Plain HTTP Request Was Sent to HTTPS Port’ in NGINX

NGINX is a popular web server used by millions of websites and organizations. It is capable of handling high traffic websites and offers tons of features. However, sometimes you may get an error ‘Plain HTTP request was sent to HTTPS port’ in NGINX. In this article, we will learn how to fix this error.


How to Fix ‘Plain HTTP Request Was Sent to HTTPS Port’ in NGINX

This error occurs when you try to configure both HTTP and HTTPS connections on the same NGINX server.

Let us say you have the following configuration in your NGINX server. We have created two server blocks one for http and the other for https. The server block for HTTP requests listens to port 80 while server block for HTTPS requests listens to port 443. All requests sent to HTTP server block are redirected to the server block that services HTTPS requests.

server{
        listen 80;
        server_name example.com www.example.com;
        return 301 https://www.example.com$request_uri;
}
server {
        listen 443 ssl http2;
        server_name example.com www.example.com;

        root   /var/www/html/example.com/;
        index index.php index.html index.htm;



        # SSL/TLS configs
        ssl on;
        ssl_certificate /etc/ssl/certs/example_com_cert_chain.crt;
        ssl_certificate_key /etc/ssl/private/example_com.key;

        include /etc/nginx/ssl.d/ssl.conf;


        location / {
                try_files $uri $uri/ /index.php?$query_string;
        }
        ...
}

When you run the above configuration, you will most likely get the error ‘Plain HTTP Request Was Sent to HTTPS Port’. This is because, when users enter HTTP URLs such as http://example.com our NGINX server will try to redirect that request to the server handling HTTPS requests. But in this case, the server expects the request to use SSL. Since this did not happen, it shows the error.

On the other hand, if the user directly enters the HTTPS URL such as https://example.com they won’t get the error.

To fix this error, you need to open NGINX configuration file and comment out the following line or set ssl directive to off, highlighted in bold in above configuration.

#ssl on 
OR
ssl off

Save and close the file. Restart NGINX server to apply changes.

# systemctl restart nginx
OR
$ sudo systemctl restart nginx

In this article, we have learnt how to fix ‘plain HTTP request was sent to HTTPS port’ error in NGINX.

Also read:

How to Block USB Storage Devices in Linux
How to Disconnect Inactive or Idle SSH Sessions
How to Enable Debugging Mode in SSH
How to Copy Column to Another Column in MySQL
How to Add Header in CSV File Using Shell Script

Leave a Reply

Your email address will not be published. Required fields are marked *