Xonima
Xonima

Reputation: 1

Nginx facing problem when proxy_pass contains undersocres

I have an Nginx server which redirect requests to another external sites. The location configuration below :

location ~* ^\/h\/([a-zA-Z0-9.-]+)\/(.*) {

        proxy_connect_timeout 20s;
        set $remote_host $1;
        proxy_set_header Host $remote_host;
        proxy_pass http://$remote_host/$2;

So , the server receive a request like :

http://localhost/h/test.com/testinguri.txt

and the proxy pass will be http://test.com/testinguri.txt

Everything works fine until we use two underscores and the server will respond with 502 :

curl -I http://localhost/h/test.com/testing_ur_i.txt

HTTP/1.1 502 Bad Gateway Server: nginx/1.10.3 Content-Type: text/html Content-Length: 173 Connection: keep-alive

The error log with debug mode :

2020/11/05 00:00:11 [error] 40859#40859: *66328156 upstream prematurely closed connection while reading response header from upstream, client: ****, server: _, request: "GET /h/test.com/testing_ur_i.txt HTTP/1.1", upstream:

The request failed after least than one second , so it is not a timeout problem. And the distant site works correctly. (i changed the real domain name by test.com for security reasons)

Thank you

Upvotes: 0

Views: 196

Answers (2)

Xonima
Xonima

Reputation: 1

So , it wasn't related to the use of underscore. The problem was the http version supported by the distant site!

Changing the http to 1.1 for the proxy-pass fixed the issue :

proxy_http_version 1.1;

Thank you.

Upvotes: 0

Emil T
Emil T

Reputation: 11

Given error actually says that connection was closed by your backend (test.com in your example).

Please try to add it to your location section:

proxy_read_timeout 300s;
proxy_connect_timeout 75s;

https://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_read_timeout

Upvotes: 1

Related Questions