Nginx with proxy_pass and local static cache won’t serve images from the proxy location

apache-2.2nginxPROXYreverse-proxyweb-server

Morning gentlemen,

So, the problem I'm having right now is that I want this setup to work:

http://qwe1.com/

This request should go to Apache and get all images from the static content folders, this works very well with this config.


http://qwe1.com/forum 

This request should be proxied to server2 (proxy.qwe1.com), this also works very well now.


http://qwe1.com/forum/example.jpg 

This request should also be proxied to server2 since it has to do with the same forum, this does NOT work today due to the static cache directives. It matches the static cache directives and tries to find the image locally on disk and gives me a 404 in the static.error.log which you can see below. If I comment out the static cache directives I get the desired setup, but I'd really want to run that cache as well of course.


I tried sorting this out with the help of rewrites, I've tried these solutions so far

        location ~* ^(?P<part_uri>/forum/.*\.(css|js|jpg|jpeg|png|swf|gif|svg|ttf|eot))$ {
            proxy_pass      http://proxy.qwe1.com/forum_en$part_uri;
            proxy_redirect  off;
            proxy_set_header        Host $host;
            access_log /var/log/nginx/TMP.log;
            error_log /var/log/nginx/TMP.log;
    }

This solution basically forwards everything from http://qwe1.com/forum to http://qwe1.com/ somehow, I've not really managed to figure out why, at least it doesn't work.

I've also tried this solution

rewrite ^(/forum/*\.(css|js|jpg|jpeg|png|swf|gif|svg|ttf|eot))$ /forum_en/$1;

    location /forum_en  {
            proxy_pass      http://proxy.qwe1.com;
            proxy_redirect  off;
            proxy_set_header        Host $host;
            access_log /var/log/nginx/TMP.log;
            error_log /var/log/nginx/TMP.log;
    }

Which gives the same symptons as the earlier one, I get redirected from http://qwe1.com/forum to http://qwe1.com/

Any ideas folks?
I have the whole config here beneath.
Thanks in advance.


## one.qwe1.com = server1
## proxy.qwe1.com = server2

server {
        listen 80;
        server_name one.qwe1.com;
        access_log /var/log/nginx/qwe1.com.access.log;
        error_log /var/log/nginx/qwe1.com.error.log;

        # proxy /forum to server2
        location /forum {
                proxy_pass      http://proxy.qwe1.com/forum_en/;
                proxy_redirect  off;
                proxy_set_header        Host $host;
                access_log /var/log/nginx/TMP.log;
                error_log /var/log/nginx/TMP.log;
        }

        # static content folders
        location ~ ^/(images|css|js|fonts) {
                root /var/www/qwe1.com/current/public;
                access_log /var/log/nginx/qwe1.com.static.access.log;
                error_log /var/log/nginx/qwe1.com.static.error.log;
        }
        # static content files
        location ~* \.(css|js|jpg|jpeg|png|swf|gif|svg|ttf|eot)$ {
                root /var/www/qwe1.com/current/public;
                access_log /var/log/nginx/qwe1.com.static.access.log;
                error_log /var/log/nginx/qwe1.com.static.error.log;
        }

        # proxy to apache
        location / {

                # proxy settings
                proxy_pass         http://127.0.0.1:8080;
                proxy_redirect     off;

                proxy_set_header   Host             $host;
                proxy_set_header   X-Real-IP        $remote_addr;
                proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;

                client_max_body_size       10m;
                client_body_buffer_size    128k;

                proxy_connect_timeout      90;
                proxy_send_timeout         90;
                proxy_read_timeout         90;

                proxy_buffer_size          4k;
                proxy_buffers              4 32k;
                proxy_busy_buffers_size    64k;
                proxy_temp_file_write_size 64k;
        }
}

Best Answer

There was a similar problem just a few days ago: the answer.

Summary:
Regular expressions have higher priority than "normal" location statements.
To change this, use the ^~ modifier on your /forum/ location like this:

location ^~ /forum/ {
    # proxy_pass to server2
}