$https

The $https variable indicates whether a request was made over HTTPS (secure) or not. — NGINX Core (HTTP)

$https NGINX Core (HTTP)

Description

The $https variable is a built-in variable in NGINX that is used to determine if the current request is being handled over HTTPS. When a request is handled over a secure connection, $https is set to 'on'. If the request is not secure (i.e., it is served over HTTP), this variable returns 'off'. This variable is particularly useful for redirecting HTTP traffic to HTTPS or for configuring conditional responses based on the security of the connection. The value of the $https variable is typically set when NGINX is configured with a proper SSL certificate and is listening on HTTPS ports. For example, if an NGINX server block specifies an SSL certificate and listens on port 443, $https will be set to 'on' for incoming requests on that block. Conversely, if there is no SSL configuration and the server is listening on port 80, the variable will be 'off'. It is important to note that this variable does not take any value other than 'on' or 'off'.

Config Example

server {
    listen 443 ssl;
    server_name example.com;
    ssl_certificate /path/to/cert.pem;
    ssl_certificate_key /path/to/key.pem;

    location / {
        if ($https = off) {
            return 301 https://$host$request_uri;
        }
        # Handle the secure request
    }
}

Subsystem

http

Cacheable

Yes

Contexts

http, server, location, if

Ensure that SSL is correctly configured; otherwise, $https will not work as expected.

The variable may not be reliable if NGINX is behind a proxy that does SSL termination; use appropriate headers to check the original connection scheme.