Nginx image display issue after optimization

I made some optimizations using gtmetrix to improve web site performance. Like enabling gzip compression etc..

When I try to test site using online services like browserling.com images are not dislayed.

https://programlama.tk

When I try to connect from server. Like 192.168.1.xx it works fine.

Related part of /etc/nginx/nginx.conf is like that :

##
# Gzip Settings
##

gzip on;

gzip_vary on;
gzip_proxied any;
# gzip_comp_level 6;
# gzip_buffers 16 8k;
# gzip_http_version 1.1;
gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

And in /etc/nginx/sites-enabled/programlama.tk I added this part.

location ~*  \.(jpg|jpeg|png|gif|ico)$ {
    expires 365d;
}

location ~*  \.(css|js)$ {
    expires 30d;
}

location ~*  \.(pdf)$ {
    expires 30d;
}

Update :

I suspect the problem is in server block in location expires part.

server {
    server_name programlama.tk;

    location /static {
        limit_except GET HEAD POST {
             deny all;
        }
        include yasaklananlar.conf;
        # alias
    }

    location / {
        limit_except GET HEAD POST {
             deny all;
        }
        include yasaklananlar.conf;
        include proxy_params;
        # proxy_pass
    }

    location ~*  \.(jpg|jpeg|png|gif|ico)$ {
        expires 365d;
    }

    location ~*  \.(css|js)$ {
        expires 30d;
    }

    location ~*  \.(pdf)$ {
        expires 30d;
    }

    # server settings

}

If I remove location expires part in server block and restart Nginx server it displays images fine.


Nginx chooses a single location to process a request.

So when a new regular expression location is added to set the expires value on an extension, it breaks both your location /static {...} and location / {...} configurations.

As an alternative to using location to set expires, you can use a map instead. See this document for details.

For example:

map $request_uri $expires {
    default                           off;
    ~*\.(jpg|jpeg|png|gif|ico)(\?|$)  365d;
    ~*\.(css|js|pdf)(\?|$)            30d;
}    

server {
    ...
    expires $expires;

    location /static { ... }
    location / { ... }
    ...
}