I
I
Ivan Ivanov2022-01-10 13:37:02
PHP
Ivan Ivanov, 2022-01-10 13:37:02

How to pass url with & symbols to nginx? from php using X-Accel-Redirect?

Nginx is used to proxy a remote server.
php code:

$url = '/internal_redirect/?url=' .
            'domain.com?test=123&test2=234' .
            '&protocol=https';

        header("X-Accel-Redirect: " . $url);


The problem is in
domain.com?test=123&test2=234
. If you leave it as in the example, then additional variables like arg_test2 will simply be passed, etc. If you use urlencode, then nginx does not automatically decode. How to deal with symbols? and & Preferably without installing additional modules. This is real?

Partial nginx code
location ~* ^/internal_redirect/ {
  internal;

  set $download_url $arg_protocol://$arg_url;

  proxy_pass $download_url;
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
dodo512, 2022-01-10
@romalu

First, just add the prefix /internal_redirect/

$url = '/internal_redirect/http://domain.com/?test=123&test2=234';

header("X-Accel-Redirect: " . $url);

Then we remove this prefix and add everything that is in $args.
location ~ ^/internal_redirect/(?<url>.+) {
    internal;
    
    proxy_pass $url$is_args$args;
}

K
ky0, 2022-01-10
@ky0

Everything after the question mark is not part of the url, but parameters. You can, for example, pass them to nginx in separate headers, and attach them to the url inside it. It does not require any exotic modules.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question