I am trying to get images not found on our staging server to automatically load from the production server. I tested this locally with the following configuration on nginx 1.6.2: And this worked as expected

server {
    root /var/www/html/test;
    index index.html index.htm;

    # Make site accessible from http://localhost/
    server_name nginx1.local;

    location / {
            # First attempt to serve request as file, then
            # as directory, then fall back to displaying a 404.
            try_files $uri $uri/ @fallback;
            # Uncomment to enable naxsi on this location
            # include /etc/nginx/naxsi.rules

    location @fallback{
            proxy_pass http://nginx2.local;


However in our production server I can't get it working. Production runs nginx 1.1.19 and I am wondering if something has changed with proxy_pass since then. Let it be known I'm new at this job and can't be held accountable yet for everything being out of date.

server {
    # Specify default_server here as catch-all (sites not in any other 'server' blocks)
    listen   80; # ipv4
    #listen   [::]:80 default ipv6only=on; # ipv6

    root /var/www/testing/public;
    index index.html index.htm /index.html; # Ending /index.html is a sort of 'internal redirect' because of the starting slash

    # Set to "" (empty) as catch-all (in conjuction with listen directive)
    server_name "testing.media.foo.com";

    # Logging Settings
    access_log /var/www/testing/logs/access.log;
    error_log /var/www/testing/logs/error.log;

    # Specify a charset
    charset utf-8;

    # Implement filename-based cache busting ala h5bp
    #include /etc/nginx/h5bpconf/cache-busting.conf;
    rewrite ^/(.+)\.v(\d+)\.(js|css|png|jpg|jpeg|gif)$ /$1.$3;

    # Include other h5bp conf files
    include /etc/nginx/h5bpconf/x-ua-compatible.conf;
    include /etc/nginx/h5bpconf/protect-system-files.conf;
    #include /etc/nginx/h5bpconf/cross-domain-ajax.conf;
    include /etc/nginx/h5bpconf/no-transform.conf;

    location / {

    ## Handle alternate domains first
    # include /var/www/testing/nginx_conf/alternate_domains.conf;

            # First attempt to serve request as file, then
            # as directory, then fall back
            try_files $uri $uri/ @fallback;

            # these h5bp configs only have an effect if try_files finds a file
            include /etc/nginx/h5bpconf/expires.conf;
            include /etc/nginx/h5bpconf/cross-domain-fonts.conf;
            # Uncomment to enable naxsi on this location
            # include /etc/nginx/naxsi.rules

    location @fallback {
            proxy_pass http://media.foo.com;
            #return 404;

    # if you don't like seeing all the errors for missing favicon.ico in root
    location = /favicon.ico { access_log off; log_not_found off; }

    # if you don't like seeing errors for a missing robots.txt in root
    location = /robots.txt { access_log off; log_not_found off; }

    # this will prevent files like .htaccess .htpassword .secret etc from being served
    # You can remove the log directives if you wish to
    # log any attempts at a client trying to access a hidden file
    #location ~ /\. { deny all; access_log off; log_not_found off; }

    #location /doc/ {
    #        alias /usr/share/doc/;
    #        autoindex on;
    #        allow;
    #        deny all;

    # Only for nginx-naxsi : process denied requests
    #location /RequestDenied {
            # For example, return an error code
            #return 418;

    #error_page 404 /404.html;

    # redirect server error pages to the static page /50x.html
    #error_page 500 502 503 504 /50x.html;
    #location = /50x.html {
    #       root /usr/share/nginx/www;


testing server is testing.media.foo.com and production server is media.foo.com if an image is not found on testing.media.foo.com I want it to load from media.foo.com rather than having to always copy images into testing from production.

edit: Here is output of nginx -V --prefix=/etc/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-client-body-temp-path=/var/lib/nginx/body --http-fastcgi-temp-path=/var/lib/nginx/fastcgi --http-log-path=/var/log/nginx/access.log --http-proxy-temp-path=/var/lib/nginx/proxy --http-scgi-temp-path=/var/lib/nginx/scgi --http-uwsgi-temp-path=/var/lib/nginx/uwsgi --lock-path=/var/lock/nginx.lock --pid-path=/var/run/nginx.pid --with-debug --with-http_addition_module --with-http_dav_module --with-http_flv_module --with-http_geoip_module --with-http_gzip_static_module --with-http_image_filter_module --with-http_mp4_module --with-http_perl_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_stub_status_module --with-http_ssl_module --with-http_sub_module --with-http_xslt_module --with-ipv6 --with-sha1=/usr/include/openssl --with-md5=/usr/include/openssl --with-mail --with-mail_ssl_module --add-module=/build/buildd/nginx-1.1.19/debian/modules/nginx-auth-pam --add-module=/build/buildd/nginx-1.1.19/debian/modules/chunkin-nginx-module --add-module=/build/buildd/nginx-1.1.19/debian/modules/headers-more-nginx-module --add-module=/build/buildd/nginx-1.1.19/debian/modules/nginx-development-kit --add-module=/build/buildd/nginx-1.1.19/debian/modules/nginx-echo --add-module=/build/buildd/nginx-1.1.19/debian/modules/nginx-http-push --add-module=/build/buildd/nginx-1.1.19/debian/modules/nginx-lua --add-module=/build/buildd/nginx-1.1.19/debian/modules/nginx-upload-module --add-module=/build/buildd/nginx-1.1.19/debian/modules/nginx-upload-progress --add-module=/build/buildd/nginx-1.1.19/debian/modules/nginx-upstream-fair --add-module=/build/buildd/nginx-1.1.19/debian/modules/nginx-dav-ext-module

  • 1
    In last config you use html5 boilerplate which overrides your naive try_files. – Alexey Ten Feb 11 '15 at 7:16
  • I've tried commenting out all those configs and creating a brand new configuration file to match what I did on my local system, nothing seems to work. – cnizzardini Feb 11 '15 at 17:58
  • Show new config. – Alexey Ten Feb 11 '15 at 17:59
  • I'm actually wondering if ngx_http_proxy_module is not installed/enabled. I updated the question with nginx -V output. The config I used nearly identical to the first one I posted (from my local test) – cnizzardini Feb 11 '15 at 18:37
  • It's enabled by default. If it's not, you'll get syntax errors, as nginx will not know proxy_* directives. – Alexey Ten Feb 11 '15 at 18:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.