proxy_pass with variable

Jeffrey Knight jeffrey.knight at gmail.com
Fri Dec 18 18:37:57 UTC 2020


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

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.nginx.org/pipermail/nginx/attachments/20201218/00f94f33/attachment.htm>


More information about the nginx mailing list