openresty, proxy_pass and normalized request_uri issue

openresty

 

I would like to give you a brief overview of the nginx reverse proxy problem and solution I have been working on for days. Actually answer was very simple.

I am using openresty(nginx + lua) reverse proxy to map http://server/$user/$server  to a docker instance IP/port by using openresty lua feature.

I used https://openresty.org/en/dynamic-routing-based-on-redis.html this article as a reference for lua queries.

My goal: I had to remove /$user/$server before sending request to proxy_pass parameter.

Here is pseudo nginx configuration:
Location / {
set target ” ;
access_by_lua_block {
some lua code to map url path to docker IP port
ngx.var.target = dockerIP:port
}

rewrite_by_lua_block {
remove /$user/$server
ngx.req.set_uri($modified_uri)

}

include /path/to/proxy.conf ;
pass_proxy $target;

}

Since I am rewriting the request_uri by stripping /pathX parameter, nginx encodes it.

This encoding/normalization breaks my nodejs client request that cause unexpected errors.
The solution: Specify modified uri as a parameter to pass_proxy host.
pass_proxy $host$modified_uri

A quote from http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_pass

A request URI is passed to the server as follows:

  • If the proxy_pass directive is specified with a URI, then when a request is passed to the server, the part of a normalized request URI matching the location is replaced by a URI specified in the directive:
    location /name/ {
        proxy_pass http://127.0.0.1/remote/;
    }
    
  • If proxy_pass is specified without a URI, the request URI is passed to the server in the same form as sent by a client when the original request is processed, or the full normalized request URI is passed when processing the changed URI:
    location /some/path/ {
        proxy_pass http://127.0.0.1;
    }
    
    
    
Advertisements
openresty, proxy_pass and normalized request_uri issue

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s