Welcome! Log In Create A New Profile

Advanced

proxy_pass with variable

Jeffrey Knight
December 18, 2020 01:40PM
Hello !

I'm trying to get up a reverse proxy where my users can pass in a url of the form

https://my.server.com?https://some.other.server.com

and it'll proxy to it.

It works perfectly with this configuration with the proxy_pass target hard coded:

```
server {
server_name my.server.com;

listen [::]:443 ssl ipv6only=on; # managed by Certbot
listen 443 ssl; # managed by Certbot

location / {
proxy_pass https://some.other.server.com;
add_header Cache-Control "public, max-age=3";

add_header 'Access-Control-Allow-Origin' "$http_origin";
add_header 'X-Frame-Options' "ALLOW FROM $http_origin";
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Vary' 'Origin';
}
}
```

testing with:
> curl -X POST https://my.server.com -H "Content-Type: application/json" -d "{\"id\": \"123\"}"

But if I swap out the proxy_pass target with a variable, I'm getting a 502 Bad Gateway.

```
server {
server_name my.server.com;

listen [::]:443 ssl ipv6only=on; # managed by Certbot
listen 443 ssl; # managed by Certbot

location / {
proxy_pass $args;
add_header Cache-Control "public, max-age=3";

add_header 'Access-Control-Allow-Origin' "$http_origin";
add_header 'X-Frame-Options' "ALLOW FROM $http_origin";
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Vary' 'Origin';
}
}
```

Testing with:
> curl -X POST https://my.server.com?https://some.other.server.com -H "Content-Type: application/json" -d "{\"id\": \"123\"}"

I'm writing the $args out to the logs:

log_format main 'ARGS: >>$args<<';
access_log /var/log/nginx/access.log main;

and it looks fine."$args" is identical to what I had hard coded, so I know that "args" is exactly the url I want to proxy_pass to.

My location is not a regular expression and according to the docs [1], variables in proxy_pass should be fair game?

-----
When variables are used in proxy_pass:
location /name/ {
proxy_pass http://127.0.0.1$request_uri;
}
In this case, if URI is specified in the directive, it is passed to the server as is, replacing the original request URI.
-----

Any help is much appreciated !
Jeff

[1] https://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_pass

_______________________________________________
nginx mailing list
nginx@nginx.org
http://mailman.nginx.org/mailman/listinfo/nginx
Subject Author Posted

proxy_pass with variable

Jeffrey Knight December 18, 2020 01:40PM

Re: proxy_pass with variable

Maxim Dounin December 18, 2020 02:14PM



Sorry, only registered users may post in this forum.

Click here to login

Online Users

Guests: 56
Record Number of Users: 6 on February 13, 2018
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready