3

im not very experienced with nginx so I am a bit lost with vhost set up. Basically my app, when serving some images serves them via php script this is very simple with apache: look for physical image -> if not found push everything in to index.php with the request string as params. No I am tryingtorun this app on nginx and everything works except the image serving via script (I just get a 404). Here is my vhost for nginx:

server {
    listen 80;

    server_name ~^(www\.)?(?<sname>.+?).subdomain.domain.com$;
    root /var/www/$sname/current/public;
    index index.html index.htm index.php;

    location / {
            try_files $uri $uri/ /index.php$is_args$args;
    }

    location ~* \.(jpg|jpeg|gif|png|bmp|ico|pdf|flv|swf|exe|html|htm|txt|css|js) {
            add_header        Cache-Control public;
            add_header        Cache-Control must-revalidate;
            expires           7d;
    }

    location ~ \.php$ {
            fastcgi_pass unix:/var/run/php/php7.1-fpm.sock;
            include fastcgi_params;
            fastcgi_index index.php;
    }

    location ~ /\.ht {
            deny all;
    }

}

It seems that it is only looking for the image in a physical location, this works for actual physical images, but not for dynamic ones where they are served vua a script. Any help or guidance is greatly appreciated.

Update: ok so i figured out that if I remove the .jpg from cache controll location, it works, but I still want to set cache headers for those dynamic image requests, so how do I make it run via php and then set the cache header after that?

  • Your jpg|jpeg|gif|png|bmp|ico|pdf|flv|swf|exe|html|htm|txt|css|js expression is so inclusive, it is not clear what URIs are excluded from requiring these additional cache control headers. If there are none, move the statements into the server block. – Richard Smith Jul 12 '17 at 11:01
  • Hi Richard, this expression is not URi based, it's for resource extensions. The way I understand is that this forces webserver to add cache heaers on all listed extensions. As for moving it in server block, I think this would help as it would not lock those extensions to only add_header action. The question then is what should the syntax for this look like if I moce them out of location and in to server? (sorry, Im rly bad withnginx syntax) – Auris Jul 12 '17 at 11:07
2

You can use this location block for your images:

location ~* \.(jpg|jpeg|gif|png|bmp)$ {
    try_files $uri $uri/ /index.php$is_args$args;

    add_header        Cache-Control public;
    add_header        Cache-Control must-revalidate;
    expires           7d;
}

You might need to modify /index.php?$is_args$args part on the try_files line so that you get the correct parameters for your script, as your initial question didn't show clearly what the parameters are you want.

Then, for rest of caching options, use this location block:

location ~* \.(ico|pdf|flv|swf|exe|html|htm|txt|css|js)$ {
    add_header        Cache-Control public;
    add_header        Cache-Control must-revalidate;
    expires           7d;
}

I also added the $ to the regex matching strings, so that only requests ending with the extension are processed by that block. For example, with your initial configuration, an URL https://example.com/path/image.jpg75783 would be processed by your location block where you specify your cache directives.

Another alternative is to set your image caching headers in your PHP script.

0

In order to add the headers and expiry to all URIs, you need to place the statements in the server block. In which case, they are inherited by every location block. For example:

server {
    ...

    add_header        Cache-Control public;
    add_header        Cache-Control must-revalidate;
    expires           7d;

    location / {
        try_files $uri $uri/ /index.php$is_args$args;
    }

    location ~ \.php$ {
        ...
    }

    location ~ /\.ht {
        deny all;
    }
}
  • Hi, thank you for your answer, I understand what you ment now. But won't this add cache heades for the "non-resource" pages like output served via php script? – Auris Jul 14 '17 at 14:25

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.