简体   繁体   English

nginx与javascript文件过期(由PHP动态生成)

[英]nginx with expires on javascript files (dynamically generated by PHP)

I have a problem with expires headers on javascript files which are generated by PHP.. 我有一个由PHP生成的javascript文件的expires头问题..

The website has two types of javascript files. 该网站有两种类型的JavaScript文件。 One part is static javascript files and one part is dynamically generated by PHP. 一部分是静态javascript文件,一部分是由PHP动态生成的。

conf without expires headers conf没有过期标题

Here no expires headers are added to the .js files (All files return HTTP 200 ) 这里没有将expires标题添加到.js文件中(所有文件都返回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 与过期标题conf

When adding a location for .js files then all dynamically generated files return HTTP 404 .js文件添加位置时,所有动态生成的文件都返回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? 如何处理带有expires头的静态和动态生成的.js文件?

All dynamically genereated javascript files are named *-php.js 所有动态生成的javascript文件都命名为*-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 PHP路由

www.example.com/ -> index.php
www.example.com/js -> static content
www.example.com/js/dynamically_generated-php.js -> js-dynamically_generated.php

For nginx, PHP is never Javascript. 对于nginx,PHP绝不是Javascript。 Nginx can't distinct between PHP which renders html and PHP which renders javascript (please correct me if I'm wrong). Nginx无法区分呈现html的PHP和呈现javascript的PHP(如果我错了请纠正我)。

So the way to go would be either to setup a seperate folder with PHP files which generate all JS (code is not tested!): 所以要走的路要么是设置一个单独的文件夹,其中包含生成所有JS的PHP文件(代码未经过测试!):

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本身发送标题:

<?php
header('Expires: '. gmdate('D, d M Y H:i:s \G\M\T', time() + (60 * 60))); // 1 hour

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM