1

Below is my load balancing configuration.my other servers contain nginx unicorn based application setup. When i tried above configuration the images are not loading. I have total three servers 1 for load balance and other 2 for applications. can some one help me with this. I am totally struck on this.

upstream backend {
    server ws1.10.10.1.1 fail_timeout=10;
    server ws2.10.10.1.2 fail_timeout=5;
}

server {

    listen 80;
    client_max_body_size 2G;
    server_name staging.xxxx.com;
    include /etc/nginx/mime.types; 
    default_type  application/octet-stream;
    location / {
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header  X_FORWARDED_PROTO $scheme;
        proxy_set_header  Host $host;
        proxy_connect_timeout 3;
        proxy_read_timeout 60;
        proxy_send_timeout 60;
        proxy_redirect false;
        proxy_max_temp_file_size 0;

        if (!-f $request_filename) {
            proxy_pass http://backend;
        }
    }

    location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico|xml)$ { }
}
Xavier Lucas
  • 13,095
  • 2
  • 44
  • 50
  • Are images directly on the filesystem or do you want to serve them through the proxied servers ? – Xavier Lucas Oct 28 '14 at 11:19
  • @XavierLucas i want to server them through proxied servers – user3585106 Oct 28 '14 at 12:03
  • @XavierLucas without adding asserts domain getting redirected to load balance servers.While adding asserts the website is not loading properly images problems – user3585106 Oct 28 '14 at 12:04
  • only one location block ever "matters" - `location ~* ^.+.` - if that location block matches, everything in `location /` is ignored/irrelevant. – AD7six Oct 28 '14 at 12:49
  • What is the actual purpose of that last empty/redacted location block? – AD7six Oct 28 '14 at 13:10

2 Answers2

2

The issue if you want to serve them through proxied servers is that the regex location block has higher priority in nginx location's search when URI matches. So, remove the last location block or write one unique fallback location and use try_files.

open_file_cache max=10 inactive=10m;
open_file_cache_valid 5m;
open_file_cache_min_uses 1;
open_file_cache_errors on;

location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico|xml)$ {
    # other stuff
    try_files /unreachable/path @fallback;
}

location / {
    # other stuff
    try_files /unreachable/path @fallback;
}

location @fallback {
    proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header  X_FORWARDED_PROTO $scheme;
    proxy_set_header  Host $host;
    proxy_connect_timeout 3;
    proxy_read_timeout 60;
    proxy_send_timeout 60;
    proxy_redirect false;
    proxy_max_temp_file_size 0;
    proxy_pass http://backend;
 }
Xavier Lucas
  • 13,095
  • 2
  • 44
  • 50
  • `i want to server them through proxied servers` I might be misunderstanding - but the loadbalancer doesn't have any files, `try_files` won't do anything useful. – AD7six Oct 28 '14 at 12:51
  • @AD7six This is a trick to serve from multiple locations to one unique fallback. Not related to local files search. – Xavier Lucas Oct 28 '14 at 13:07
  • An example would help. I'm far from a nginx noob and I don't know what you mean. – AD7six Oct 28 '14 at 13:09
  • @XavierLucas i got something here i changed my settings in nginx confi and added location ~ ^/assets/ { root /var/www/project_name/app/images; ( here what happening is the image files should load from assets/images/ but here its loading from /assets/images/asstes/ (assets adding again to images) error is like ( app/assets/images/assets/error_icon.png) How can i fix this issue. – user3585106 Oct 28 '14 at 13:36
  • @XavierLucas server { listen 80; server_name staging.xxxx.com; location ~ ^/assets/ { root /var/www/proname/app/assets/images; expires max; add_header Cache-Control public; } – user3585106 Oct 28 '14 at 13:44
  • @AD7six Here it is... – Xavier Lucas Oct 28 '14 at 14:08
  • What you are doing there, is telling nginx to check the local file system *on the load balancer* before passing to the proxy. Does that really sound like a good 'trick' to use? – AD7six Oct 28 '14 at 20:10
  • @AD7six And ? If your eyes can't catch the logic, I updated the answer for better understanding of the concept for people unfamiliar with nginx that can't imagine what comes next. Still interested if you have a better way to go. – Xavier Lucas Oct 28 '14 at 20:36
  • You are using try files, instead of just putting the config in two places (or putting the config in a file and including it twice) _at the cost_ of making nginx repeatedly try and access a location that won't exist. It's a trick to make writing the config easier with a run-time cost. – AD7six Oct 28 '14 at 20:51
  • @AD7six It has no cost. File lookup errors are **cached** so it's memory access. Do you call a lookup syscall every 5 mins a *cost* ?? – Xavier Lucas Oct 28 '14 at 20:54
  • I wondered if you'd point that out - it's still an abuse of try files which at the very least I wouldn't call a best practice =). – AD7six Oct 28 '14 at 20:55
  • @AD7six So what's *your* solution ? Including a file over and over in each location so you can avoid one `open()` call? You know what, put the cache to 365 days and with this solution you will already avoid one compared to `include` usage on 2 locations. Haha – Xavier Lucas Oct 28 '14 at 21:04
  • @XavierLucas Thank you. I fixed it myself here i dont need to add try_files but load balancer url should be added in both nodes (rails). – user3585106 Oct 29 '14 at 05:58
0

I fixed it myself the problem is i should add load balance url in application.rb as well as seeeion_store.rb mistakenly i added localhost so it fails to get images now its fixed.