$server_protocol
The $server_protocol variable contains the protocol used for the current request, typically HTTP or HTTPS. — NGINX Core (HTTP)
Description
The $server_protocol variable in NGINX is dynamically set during the processing of a request and returns the protocol version that the client used to connect to the server. This variable is particularly useful for logging, conditional configurations, and response handling, as it allows you to distinguish between different protocol versions such as HTTP/1.0, HTTP/1.1, or HTTP/2.0. Typically, the variable is set during the server's request phase, becoming available in various contexts, including http, server, and location blocks. When a request arrives at the NGINX server, the module evaluates the incoming request details and populates the $server_protocol variable accordingly. Its typical values are strings like "HTTP/1.1", "HTTP/2", etc., but it reflects the exact protocol that was negotiated during the connection establishment phase with the client. Understanding the $server_protocol variable is important for developers and system administrators as it can serve as a basis for security policies or functionality toggles. For instance, you might want to allow or deny access based on whether a connection was established over HTTPS or HTTP. Logging the protocol can also provide valuable insights for performance and security analysis.
Config Example
server {
listen 80;
server_name example.com;
location / {
add_header X-Protocol $server_protocol;
}
}Subsystem
httpCacheable
YesContexts
http, server, location, ifEnsure that the variable is used in the right context where it's available (http, server, location).
Do not confuse $server_protocol with similar variables like $http_protocol which holds HTTP version information that can be different in certain scenarios.