Nginx request port

nginxssl

I have an nginx server with SSL enabled. As it operates on a different port (than 443), sometimes it receives plain HTTP requests instead of HTTPS. I'd like to set up a redirection for that to replace the scheme automatically, I tried this code:

error_page 497 https://$host:$server_port$request_uri;

But the problem here that as the server operates in a VM and the port is forwarded to a different port, it redirects to an invalid port (from which the server got the request).

My question is: how can I parse/get the port from the request rather than the port from the server received the request?

Best Answer

Hopefully not too little too (eight months!) late. I had a similar question myself, to use the original request port in nginx.conf.

nginx $http_name variable

$http_name: arbitrary request header field; the last part of a variable name is the field name converted to lower case with dashes replaced by underscores

$http_host should therefore contain the request 'Host' header, if that helps.

e.g. localhost:8020