Nginx: disable HTTP responses for SSL port completely

httpsnginx

I have a private HTTPS server using nginx. Therefore, I don't care about browser compatibility nor HTTP to HTTPS redirection; I just want it to work in my environment and nowhere else. I have already configured it with "listen 17648 ssl;". Whenever I try to connect to it using plain HTTP, I get the infamous "The plain HTTP request was sent to HTTPS port" response.

Is there any way to prevent nginx from sending any response at all when a HTTP request is sent to a HTTPS port? I would like nginx to simply close the connection if the request is not SSL, or maybe return some SSL-level error but no plain HTTP response at all, not even an error response.

Best Answer

From Nginx documentation about the return clause:

The non-standard code 444 closes a connection without sending a response header.

So I've tried to catch the error 497 "HTTP to HTTPS" and tried to return the 444 this way:

error_page 497 =444 @close;

location @close {
   return 444;
}

Unfortunately this leads to a "pending" state due to this bug

Using the workaround proposed by the developer it seems to work:

error_page 497 =444 @close;

location @close {
   return 0;
}

Try if this fits your needs!