dimzak
dimzak

Reputation: 2571

Nginx proxy_pass all url parameters

I want to proxy requests like these: http://myproxy.com/api/folder1/result1?test=1
, http://myproxy.com/api/folder3447/something?var=one

to the equivalent destinations: http://destination.com/folder1/result1?test=1 and http://destination.com/folder3447/something?var=one, practically only the domain changes and all subfolders and params are preserved

location in config looks like:

location ~* ^/api/(.*) {
    proxy_pass http://destination.com/$1$is_args$args;
    proxy_redirect off;
    proxy_http_version 1.1;
    proxy_set_header Connection "";
    proxy_set_header  X-Real-IP  $remote_addr;
    proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
    #proxy_set_header  Host $http_host;
  }

Upvotes: 2

Views: 17671

Answers (1)

Joshua DeWald
Joshua DeWald

Reputation: 3209

You should be able to simplify your configuration slightly:

location /api/ {
    // Note the slash at end, which with the above location block
    // will replace "/api/" with "/". This will not work with regex
    // locations
    proxy_pass http://destination.com/;
    proxy_redirect off;
    proxy_http_version 1.1;
    proxy_set_header Connection "";
    proxy_set_header  X-Real-IP  $remote_addr;
    proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
}

Upvotes: 6

Related Questions