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

我有一个由PHP生成的javascript文件的expires头问题.@H_403_2@

该网站有两种类型的JavaScript文件.一部分是静态javascript文件,一部分是由PHP动态生成的.@H_403_2@

conf没有过期标题@H_403_2@

这里没有将过期标题添加到.js文件中(所有文件都返回HTTP 200)@H_403_2@

@H_403_2@

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@H_403_2@

为.js文件添加位置时,所有动态生成文件都会返回HTTP 404@H_403_2@

@H_403_2@

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";
}

如何处理带有expires头的静态和动态生成的.js文件?@H_403_2@

所有动态生成的javascript文件都命名为* -PHP.js@H_403_2@

文件结构@H_403_2@

@H_403_2@

/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路由@H_403_2@

@H_403_2@

www.example.com/ -> index.PHP
www.example.com/js -> static content
www.example.com/js/dynamically_generated-PHP.js -> js-dynamically_generated.PHP
最佳答案
对于Nginx,PHP绝不是Javascript. Nginx无法区分呈现html的PHP和呈现javascript的PHP(如果我错了,请纠正我).@H_403_2@

因此,要做的就是设置一个单独的文件夹,其中包含生成所有JS的PHP文件(代码未经过测试!):@H_403_2@

@H_403_2@

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;
}

…或者使用PHP本身发送标题:@H_403_2@

@H_403_2@

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

相关文章

一、Linux下Nginx的安装 1.去官网 http://nginx.org/download/下载对应的Nginx安装包,推荐使...
一、空格:默认匹配、普通匹配 location / { root /home; } 二、= :精确匹配(表示匹配到 /home/resou...
``` nginx -c 配置文件路径 ``` ``` /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.con...
前言 nginx可所谓是如今最好用的软件级别的负载均衡了。通过nginx的高性能,并发能力强,占用内存下的特...
1.ngnix概念 Nginx是一款高性能的http 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器。由俄...
博客园从今天上午就开始报502错误 , 他的原因还不知道 , 暂时先说下我们遇到502的排查情况 最大的可能性...