-2

I have one server with nginx-frontend and Apache-backend.

I need to set up nginx to return images from the other two servers randomly like

  • http://example.com/images/*.jpg -> http://i1.example.com/*.jpg
  • http://example.com/images/*.jpg -> http://i2.example.com/*.jpg

image folder is the same on both servers.

I would be grateful for any advice.

6
  • So you have 3 servers?
    – Drifter104
    Dec 18, 2015 at 12:00
  • Yes. in total 3 servers.
    – garrykiev
    Dec 18, 2015 at 12:02
  • It's unclear what you want.
    – Alexey Ten
    Dec 18, 2015 at 12:02
  • Do you want to basically load balance between the two image servers? So first request goes to i1.example.com and next request goes to i2.example.com or when you say random you want it to get a random file from those to locations?
    – Drifter104
    Dec 18, 2015 at 12:07
  • first request goes to i1.example.com and next request goes to i2.example.com
    – garrykiev
    Dec 18, 2015 at 12:12

1 Answer 1

0

You would use the upstream directive to do this.

Sample below, you would add in the rest of your server config etc

upstream backend {
    server i1.example.com;
    server i2.example.com;
}

location /images/ {
    proxy_pass http://backend/;
    #rest of config
}

It is possible to do more then 50/50 split by using the weight function but without it the default weight is used which is 1.

Docs

5
  • This is for anything in the /images/ directory, if you want just jpg it would need a small adjustment
    – Drifter104
    Dec 18, 2015 at 12:19
  • anything in the /images/
    – garrykiev
    Dec 18, 2015 at 12:20
  • @garrykiev Then this will do it
    – Drifter104
    Dec 18, 2015 at 12:21
  • @Drifter104, fixed code.
    – Alexey Ten
    Dec 18, 2015 at 12:28
  • @AlexeyTen thanks, I normally forget the ; not the /
    – Drifter104
    Dec 18, 2015 at 12:34

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .