Using proxy_http_version in Nginx

nginx proxy http version
25 January 2025

If you want to consider Nginx's performance as a proxy, you should be aware that different components play a role in this process. One of these components is the ngx_http_proxy_module, which is responsible for transferring requests and responses from one server to another server. One of the interesting features of this module is the proxy_http_version directive. With this directive, we can specify the HTTP protocol version that will be used during the connection establishment with the main server.


The HTTP protocol version can affect the behavior of requests sent to the server. For example, for servers that perform specific identification based on the protocol version being used, it is essential to use the version that is expected. In some cases, using an outdated version might prevent receiving correct responses from the server.


Now let's look at the application of this directive. Suppose you have a website that is connected to another server and this server expects to use HTTP/1.1. In this case, you should ensure that Nginx sends requests with this version to avoid connection issues.


For example, by using the proxy_http_version directive, we might have the following code snippet. Now let's see how we can use this directive and what effect it has on Nginx's performance.


location /api {
proxy_pass http://backend-server;
proxy_http_version 1.1;
proxy_set_header Connection "upgrade";
}

In this code, you can see a section of Nginx configuration that clearly has the proxy_http_version directive in it.


location /api {
proxy_pass http://backend-server;
proxy_http_version 1.1;
proxy_set_header Connection "upgrade";
}

Code Explanation


location /api

This line defines that every request to the address /api should be processed by Nginx.

proxy_pass http://backend-server;

This line specifies that requests should be sent to the backend server.

proxy_http_version 1.1;

This directive tells Nginx to use HTTP/1.1 protocol for communication with the backend server.

proxy_set_header Connection "upgrade";

This line is a header that tells Nginx to add a specific header to the request so that under some scenarios, such as WebSocket, it works effectively.

FAQ

?

What is proxy_http_version and why should we use it?

?

Which HTTP versions can be used?

?

When should I switch from HTTP/1.0 to HTTP/1.1?

?

What issues can an incorrect proxy_http_version cause?