Hi all!
I use php-fpm together with nginx.
My PHP app serves files which have hashed filenames and no filename extension from a specific subdirectory url, e.g /files/hash/31b4ba4a0dc6536201c25e92fe464f85
I would like to be able to set, for example, a separate ’expires’ value to these files with nginx (using a separate location block?). Is that achiavable?
server {
listen 443 ssl http2;
server_name my.site.com;
root /var/www/vhosts/my.site.com/site/;
set $ngspage /index.php?ngspage=$uri$is_args&$args;
# PHP file processing configuration
location ~ [^/]\.php(/|$) {
set $location_name php;
fastcgi_pass php74;
include fastcgi_params;
fastcgi_index index.php;
# Regex to split $uri to $fastcgi_script_name and $fastcgi_path
fastcgi_split_path_info ^(.+?\.php)(/.*)$;
# Bypass the fact that try_files resets $fastcgi_path_info
# see: http://trac.nginx.org/nginx/ticket/321
set $path_info $fastcgi_path_info;
# Check that the PHP script exists before passing it
try_files $ngspage $fastcgi_script_name =404;
}
Can anyone help me with this?
> On 8. May 2023, at 8.49, Palvelin Postmaster via nginx <nginx at nginx.org> wrote:
>
> Hi all!
>
> I use php-fpm together with nginx.
>
> My PHP app serves files which have hashed filenames and no filename extension from a specific subdirectory url, e.g /files/hash/31b4ba4a0dc6536201c25e92fe464f85
>
> I would like to be able to set, for example, a separate ’expires’ value to these files with nginx (using a separate location block?). Is that achiavable?
>
> server {
> listen 443 ssl http2;
> server_name my.site.com;
> root /var/www/vhosts/my.site.com/site/;
> set $ngspage /index.php?ngspage=$uri$is_args&$args;
>
> # PHP file processing configuration
> location ~ [^/]\.php(/|$) {
> set $location_name php;
> fastcgi_pass php74;
> include fastcgi_params;
> fastcgi_index index.php;
>
> # Regex to split $uri to $fastcgi_script_name and $fastcgi_path
> fastcgi_split_path_info ^(.+?\.php)(/.*)$;
>
> # Bypass the fact that try_files resets $fastcgi_path_info
> # see: http://trac.nginx.org/nginx/ticket/321
> set $path_info $fastcgi_path_info;
>
> # Check that the PHP script exists before passing it
> try_files $ngspage $fastcgi_script_name =404;
> }
On Mon, May 15, 2023 at 12:46:14PM -1000, Palvelin Postmaster via nginx wrote:
> > On 8. May 2023, at 8.49, Palvelin Postmaster via nginx <nginx at nginx.org> wrote:
Hi there,
> > I use php-fpm together with nginx.
> >
> > My PHP app serves files which have hashed filenames and no filename extension from a specific subdirectory url, e.g /files/hash/31b4ba4a0dc6536201c25e92fe464f85
> >
> > I would like to be able to set, for example, a separate ’expires’ value to these files with nginx (using a separate location block?). Is that achiavable?
In principle, yes. So long as the requests use different urls (excluding
query string).
In practice: from the words here, it is not entirely clear to me what
your overall application is doing.
Maybe you can have a location{} dedicated to these file-requests; or maybe
it would be "cleaner" for the php side to add the extra Expires header.
Can you show one or two sample requests that are made to nginx that you
do want to have this extra Expires header; and one or two that you do
not want to have this extra Expires header?
The aim is to come up with a location{} block that matches only the
requests that you want, if that is possible.
Thanks,
f
--
Francis Daly francis at daoine.org
> On 15. May 2023, at 17.15, Francis Daly <francis at daoine.org> wrote:
>
> On Mon, May 15, 2023 at 12:46:14PM -1000, Palvelin Postmaster via nginx wrote:
>>> On 8. May 2023, at 8.49, Palvelin Postmaster via nginx <nginx at nginx.org> wrote:
>
> Hi there,
>
>>> I use php-fpm together with nginx.
>>>
>>> My PHP app serves files which have hashed filenames and no filename extension from a specific subdirectory url, e.g /files/hash/31b4ba4a0dc6536201c25e92fe464f85
>>>
>>> I would like to be able to set, for example, a separate ’expires’ value to these files with nginx (using a separate location block?). Is that achiavable?
>
> In principle, yes. So long as the requests use different urls (excluding
> query string).
>
> In practice: from the words here, it is not entirely clear to me what
> your overall application is doing.
>
> Maybe you can have a location{} dedicated to these file-requests; or maybe
> it would be "cleaner" for the php side to add the extra Expires header.
>
> Can you show one or two sample requests that are made to nginx that you
> do want to have this extra Expires header; and one or two that you do
> not want to have this extra Expires header?
>
> The aim is to come up with a location{} block that matches only the
> requests that you want, if that is possible.
Francis,
thanks for your response!
I wonder if you saw the server block that I had included in my original message? Doesn’t it answer your question regarding all the other requests?
My goal is to serve only requests which include URI /files/hash/* using a separate location block. Everything else should be served by the default location block I included in my previous message.
--
Palvelin.fi Hostmaster
postmaster at palvelin.fi
On Thu, May 18, 2023 at 09:14:42PM -0700, Palvelin Postmaster via nginx wrote:
Hi there,
> My goal is to serve only requests which include URI /files/hash/*
> using a separate location block. Everything else should be served by
> the default location block I included in my previous message.
Untested, but would
location ^~ /files/hash/ {
fastcgi_pass php74;
fastcgi_param SCRIPT_FILENAME /var/your-php-script.php;
expires 10d;
}
meet what your goal is?
Adjust the fastcgi_param value to whatever your fastcgi server needs.
The important part is probably the "location" line that matches
all-and-only these requests.
Good luck with it,
f
--
Francis Daly francis at daoine.org