With nginx, how to redirect request parameters?

upstream apache { server 127.0.0.1:8080; } server{ location ~* ^/service/(.*)$ { proxy_pass http://apache/$1; proxy_redirect off; } } 

Above, the fragment will redirect part of the requests inside this URL, including the line β€œservice” to another server, but does not include the request parameters.

+51
parameters nginx
Nov 15 '11 at 2:13
source share
6 answers

From the proxy_pass documentation:

In a special case, variables are used in the proxy_pass statement: the requested URL is not used, and you are solely responsible for creating the destination URL yourself.

Since you use $ 1 for the purpose, nginx relies on you to tell you exactly what to pass. You can fix this in two ways. Firstly, stripping the start of uri with a proxy byte is trivial:

 location /service/ { # Note the trailing slash on the proxy_pass. # It tells nginx to replace /service/ with / when passing the request. proxy_pass http://apache/; } 

Or, if you want to use the regex location, just include args:

 location ~* ^/service/(.*) { proxy_pass http://apache/$1$is_args$args; } 
+80
Nov 15 '11 at 2:44
source share

I am using a slightly modified version of the second kolbyjack approach with ~ instead of ~* .

 location ~ ^/service/ { proxy_pass http://apache/$uri$is_args$args; } 
+16
Jul 25 '13 at 13:48
source share

I changed @kolbyjack code to make it work for

 http://website1/service http://website1/service/ 

with parameters

 location ~ ^/service/?(.*) { return 301 http://service_url/$1$is_args$args; } 
+4
Sep 24 '15 at 21:09
source share

you need to use rewrite to pass parameters using proxy_pass here is an example i made to deploy angularjs application for s3

S3 Static Web Hosting All the way to Index.html

adopted for your needs will be something like

 location /service/ { rewrite ^\/service\/(.*) /$1 break; proxy_pass http://apache; } 

if you want to finish http://127.0.0.1:8080/query/params/

if you want to finish at http://127.0.0.1:8080/service/query/params/ you will need something like

 location /service/ { rewrite ^\/(.*) /$1 break; proxy_pass http://apache; } 
+3
May 24 '16 at 7:30 a.m.
source share

github gist https://gist.github.com/anjia0532/da4a17f848468de5a374c860b17607e7

 #set $token "?"; # deprecated set $token ""; # declar token is ""(empty str) for original request without args,because $is_args concat any var will be `?` if ($is_args) { # if the request has args update token to "&" set $token "&"; } location /test { set $args "${args}${token}k1=v1&k2=v2"; # update original append custom params with $token # if no args $is_args is empty str,else it "?" # http is scheme # service is upstream server #proxy_pass http://service/$uri$is_args$args; # deprecated remove `/` proxy_pass http://service$uri$is_args$args; # proxy pass } #http://localhost/test?foo=bar ==> http://service/test?foo=bar&k1=v1&k2=v2 #http://localhost/test/ ==> http://service/test?k1=v1&k2=v2 
0
Oct 16 '17 at 8:56 on
source share

To redirect Without a query string, add the following lines to the server block in the listening port line

if ($uri ~ .*.containingString$) { return 301 https://$host/$uri/; }

With query string

if ($uri ~ .*.containingString$) { return 301 https://$host/$uri/?$query_string; }

-one
Jun 06 '17 at 8:24
source share