D
D
Denis Dormadekhin2018-07-02 14:03:45
Nginx
Denis Dormadekhin, 2018-07-02 14:03:45

How to correctly register a redirect with regular expressions and proxy_pass through NGINX ??

They suggested that in order to receive data from another domain, with a cors error, you can use proxying through NGINX using the proxy_pass
config:

location ~* ^.*\/proxy\/(?<pschema>.*)?\/(?<phost>.*)?\/.*$ {
        set $adr $pschema://$phost;
        
        rewrite ^.*\/proxy\/.*?\/.*?(\/.*)$ $1 break;

        proxy_pass $adr;

        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $phost;
        proxy_set_header X-NginX-Proxy true;
        proxy_redirect off;
        proxy_connect_timeout 1;
        proxy_intercept_errors on;
        expires 30;
        break;
    }

proxying must be done as follows: so that when requested:
https://my.domain.ru/proxy/https/api.binance.com/api/v1/time

data was obtained from the following address:
https://api.binance.com/api/v1/time
I saw such a solution in this question,
but when I request it, I get a 500 NGINX error

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Danila Vershinin, 2018-07-08
@dormadekhin

Correct like this:

location ~* ^/proxy/(?<pschema>https?)/(?<phost>[\w.]+)(?<puri>/.*) {
    set $adr $pschema://$phost;
    rewrite .* $puri break;

    proxy_pass $adr;

    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $phost;
    proxy_set_header X-NginX-Proxy true;
    proxy_redirect off;
    proxy_connect_timeout 1;
    proxy_intercept_errors on;
    expires 30;
}

Own original source :)
PS regex was wrong, and by the way, you don't need to escape forward slashes, since delimiter is not defined.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question