大网数据广告活动
服务器运维问答

Nginx proxy pass url from get argument

· 52CE 我爱测

Let's say I have server example.com with nginx. I want to make a proxy which will proxy pass URL given as part of request URI or GET parameter (it may contain query string). So e.g. I want nginx to resolve http://www.google.pl/im…

问题原文

原帖链接 · sebastiansito · CC BY-SA 3.0

Let's say I have server example.com with nginx.

I want to make a proxy which will proxy pass URL given as part of request URI or GET parameter (it may contain query string).

So e.g. I want nginx to resolve http://www.google.pl/image.png?x=y when GET example.com/proxy/http://www.google.pl/image.png?x=y request is made.

I've tried sth like this:

location /proxy {
    rewrite /proxy(.*) /$1 break;

  resolver 8.8.8.8;
  proxy_pass http://$arg_host/$arg_uri?$query_string;
}

Maybe I want redirect? Let's say I want to serve images which are on different server as they are mine (on my domain).

社区高分解答

原帖链接 · Troy Morehouse · CC BY-SA 3.0

Try this:

location ~ /proxy/(http://.*) {
    resolver 8.8.8.8;
    proxy_pass http://$1$is_args$args;
}

No explicit rewriting needed. $is_args will be set to ? if the request line has arguments, or an empty string otherwise, and $args will contain the parameters in the request (excluding the ?) or an empty string if there were no query parameters. $1 will match the part of the URI enclosed in ( ... ) in the location regex.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2015-02-27)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。