Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
706 views
in Technique[技术] by (71.8m points)

cors - NGINX proxy_pass to URL within URL

I am very green with NGINX. I am trying to make a generic CORS proxy to any URI. I would like the proxy to work by having the client pass the URI they need into the proxy URI.

Ex. http://host:port/Proxy/https://www.google.com

I would want NGINX to proxy_pass to just the https://www.google.com portion, but I can't seem to quite get it: it always wants to proxy_pass to /Proxy/https://www.google.com. It does append the appropriate headers (when I did simpler tests), so that part is working. I just cannot figure out the URI part.

This is my current configuration.

server {
    add_header 'Access-Control-Allow-Origin' '*';
    add_header 'Access-Control-Allow-Credentials' 'true';
    add_header 'Access-Control-Allow-Headers' '*';
    add_header 'Access-Control-Allow-Methods' '*';

    location /Proxy/ {
        proxy_pass_request_headers on;
        proxy_pass $1;
    }
}

This is after many different configurations, regex, rewrites, etc.

Any help would be most appreciated.

UPDATE

server {
    add_header 'Access-Control-Allow-Origin' '*';
    add_header 'Access-Control-Allow-Credentials' 'true';
    add_header 'Access-Control-Allow-Headers' '*';
    add_header 'Access-Control-Allow-Methods' '*';

    location / {
        proxy_pass_request_headers on;
        proxy_pass $request_uri;
    }
}

If I request http://host:port/https://wwww.somesite.com it now tries to proxy_pass to /https://www.somesite.com (with a leading slash). This, of course, fails. If I can remove the leading slash, then I am golden. Of course, if I can have it use /Proxy/someUri that would be better, but I am willing to make this compromise.

question from:https://stackoverflow.com/questions/65941739/nginx-proxy-pass-to-url-within-url

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...