0
0
NginxHow-ToBeginner · 3 min read

How to Use proxy_http_version in Nginx for Proxy Requests

Use the proxy_http_version directive inside an Nginx location or server block to specify the HTTP version (usually 1.0 or 1.1) for proxy requests. This controls how Nginx communicates with the backend server when acting as a proxy.
📐

Syntax

The proxy_http_version directive sets the HTTP version used for proxying requests to the backend server. It accepts either 1.0 or 1.1.

  • proxy_http_version 1.0; - Use HTTP/1.0 for proxy requests.
  • proxy_http_version 1.1; - Use HTTP/1.1 for proxy requests (default and recommended for keep-alive support).
nginx
proxy_http_version 1.1;
💻

Example

This example shows how to set proxy_http_version to 1.1 inside a location block to enable HTTP/1.1 proxying with keep-alive connections to the backend server.

nginx
server {
    listen 80;
    server_name example.com;

    location / {
        proxy_pass http://backend_server;
        proxy_http_version 1.1;
        proxy_set_header Connection "";
    }
}
⚠️

Common Pitfalls

Common mistakes when using proxy_http_version include:

  • Setting proxy_http_version 1.1; but not clearing the Connection header, which can cause backend connection issues.
  • Using HTTP/1.0 when the backend requires HTTP/1.1 features like chunked transfer encoding or keep-alive.
  • Not matching the backend server's supported HTTP version, leading to failed or slow proxying.

Always pair proxy_http_version 1.1; with proxy_set_header Connection ""; to avoid unwanted connection headers.

nginx
location / {
    proxy_pass http://backend_server;
    proxy_http_version 1.1;
    # Wrong: missing this line can cause issues
    # proxy_set_header Connection "";
}

# Correct usage:
location / {
    proxy_pass http://backend_server;
    proxy_http_version 1.1;
    proxy_set_header Connection "";
}
📊

Quick Reference

DirectiveDescriptionTypical Value
proxy_http_versionSets HTTP version for proxy requests1.1
proxy_set_header ConnectionControls Connection header for backend"" (empty string)
proxy_passDefines backend server URLhttp://backend_server

Key Takeaways

Use proxy_http_version to specify HTTP version for proxy requests in Nginx.
Set proxy_http_version to 1.1 for better backend connection features like keep-alive.
Always clear the Connection header with proxy_set_header Connection "" when using HTTP/1.1.
Match proxy_http_version with your backend server's supported HTTP version.
Incorrect proxy_http_version or headers can cause proxy failures or slow responses.