Dynamic Upstream

bhargava nginx-forum at forum.nginx.org
Fri Apr 28 12:05:04 UTC 2017

Pupose: Need to create a local proxy to an image cdn (Beats the purpose of
having a CDN, but can't help!!)

Setup: I have a cdn setup with multiple endpoints say t1.mycdn.com,
t2.mycdn.com and t3.mycdn.com. I have a website (foobar.com) which uses
images from these cdns. Because of some requirement, I need to have
localized path to images (say https://foobar.com/images/sellers/s1/22.jpg).
I need to proxy pass it to my 3 cdn endpoints with equal weight. Along the
way, I need to change the URL too,
https://foobar.com/images/sellers/s1/22.jpg will be fetched from
t1.mycdn.com/img/slr/s1/22.jpg OR t2.mycdn.com/img/slr/s1/22.jpg OR
t3.mycdn.com/img/slr/s1/22.jpg with equal probability.

I have compiled nginx with the following config
configure arguments: --prefix=/opt/nginx
--pid-path=/home/ec2-user/pids/nginx.pid --user=ec2-user --group=ec2-user
--without-http_autoindex_module --without-http_geo_module
--without-http_memcached_module --without-http_scgi_module
--without-http_uwsgi_module --with-http_realip_module
--with-http_addition_module --with-http_xslt_module --with-http_geoip_module
--with-http_sub_module --with-http_gzip_static_module
--with-http_stub_status_module --with-http_secure_link_module
--without-mail_pop3_module --with-http_v2_module --without-mail_imap_module
--without-mail_smtp_module --with-http_ssl_module

Current Approach: I have the following block
location ~* ^/images/sellers/[^/]+/[^/]+$ {
  error_log /tmp/images.log debug;
  set $img_host "";
  set $img_url "";
  rewrite_by_lua '
    ngx.var.img_host = "t"..math.random(1,3)..".mycdn.com"
    ngx.var.img_url = string.gsub(ngx.var.uri, "/images/sellers",
  proxy_set_header HOST $img_host;
  proxy_pass https://$img_host$img_url;

When I try to fetch the image, I keep getting the following error

invalid URL prefix in "https://"

I tried changing https to http too. Doesn't help. Can you please let me know
where am I going wrong here ?


Posted at Nginx Forum: https://forum.nginx.org/read.php?2,273963,273963#msg-273963

More information about the nginx mailing list