nginx with expires on javascript files (dynamically generated by PHP)
Asked Answered
T

1

8

I have a problem with expires headers on javascript files which are generated by PHP..

The website has two types of javascript files. One part is static javascript files and one part is dynamically generated by PHP.

conf without expires headers

Here no expires headers are added to the .js files (All files return HTTP 200)

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

location ~ \.php$ {
    include  /var/ini/nginx/fastcgi.conf;
    fastcgi_pass  php;
    fastcgi_param  SCRIPT_FILENAME /var/www/index.php;
}

conf with expires headers

When adding a location for .js files then all dynamically generated files return HTTP 404

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

location ~ \.php$ {
    include  /var/ini/nginx/fastcgi.conf;
    fastcgi_pass  php;
    fastcgi_param  SCRIPT_FILENAME /var/www/dyndev.dk/public/secure/index.php;
}

location ~ \.(js|css)$ {
    expires 1y;
    add_header Cache-Control "public";
}

How to handle both the static and dynamically generated .js files with expires headers?

All dynamically genereated javascript files are named *-php.js

File structure

/var/www/public/index.php # All none-static file requests are pointed to index.php
/var/www/public/js/main.js # Static files
/var/www/js-dynamically_generated.php # This file is outside the public www, but is routed by PHP since the file doesn't exists inside the public /js

PHP routing

www.example.com/ -> index.php
www.example.com/js -> static content
www.example.com/js/dynamically_generated-php.js -> js-dynamically_generated.php
Tress answered 19/11, 2015 at 20:26 Comment(2)
if you create js with php why you don't add header with php functions?Spy
Have updated my questionTress
P
6

For nginx, PHP is never Javascript. Nginx can't distinct between PHP which renders html and PHP which renders javascript (please correct me if I'm wrong).

So the way to go would be either to setup a seperate folder with PHP files which generate all JS (code is not tested!):

location ~ \normal_php/.php$ {
    include  /var/ini/nginx/fastcgi.conf;
    fastcgi_pass  php;
    fastcgi_param  SCRIPT_FILENAME /var/www/dyndev.dk/public/secure/index.php;
}

location ~ \js_php/.php$ {
    expires 1y;
    add_header Cache-Control "public";

    include  /var/ini/nginx/fastcgi.conf;
    fastcgi_pass  php;
    fastcgi_param  SCRIPT_FILENAME /var/www/dyndev.dk/public/secure/index.php;
}

...or send the header with PHP itself:

<?php
header('Expires: '. gmdate('D, d M Y H:i:s \G\M\T', time() + (60 * 60))); // 1 hour
Putandtake answered 24/11, 2015 at 10:1 Comment(4)
But how to I put expires headers on .js files? The problem is that when I add the location for .js files all dynamically genereated .js files return 404. See my question in the second confTress
Or are you saying that all php generated js files should be named dynamic_js.php instead of dynamic_js-php.jsTress
Yes, the second comment. It would be even better to generate your assets via a build job, so you have a PHP script which generates static .js files.Putandtake
Good idea.. Building static files would also be the fastest solution! :)Tress

© 2022 - 2024 — McMap. All rights reserved.