nginx 在 javascript 个文件上过期(由 PHP 动态生成)
nginx with expires on javascript files (dynamically generated by PHP)
我在 PHP 生成的 javascript 文件上遇到 expires
headers 问题..
该网站有两种类型的 javascript 文件。一部分是静态 javascript 文件,一部分是由 PHP.
动态生成的
conf 没有过期 headers
这里没有 expires
headers 添加到 .js
文件(所有文件 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 过期 headers
为 .js
个文件添加位置时,所有动态生成的文件 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";
}
如何使用 expires
headers 处理静态和动态生成的 .js
文件?
所有动态生成的 javascript 文件都命名为 *-php.js
文件结构
/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路由
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(如果我错了,请纠正我)。
所以要走的路是设置一个单独的文件夹,其中包含 PHP 生成所有 JS 的文件(代码未测试!):
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;
}
...或将 header 与 PHP 本身一起发送:
<?php
header('Expires: '. gmdate('D, d M Y H:i:s \G\M\T', time() + (60 * 60))); // 1 hour
我在 PHP 生成的 javascript 文件上遇到 expires
headers 问题..
该网站有两种类型的 javascript 文件。一部分是静态 javascript 文件,一部分是由 PHP.
动态生成的conf 没有过期 headers
这里没有 expires
headers 添加到 .js
文件(所有文件 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 过期 headers
为 .js
个文件添加位置时,所有动态生成的文件 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";
}
如何使用 expires
headers 处理静态和动态生成的 .js
文件?
所有动态生成的 javascript 文件都命名为 *-php.js
文件结构
/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路由
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(如果我错了,请纠正我)。
所以要走的路是设置一个单独的文件夹,其中包含 PHP 生成所有 JS 的文件(代码未测试!):
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;
}
...或将 header 与 PHP 本身一起发送:
<?php
header('Expires: '. gmdate('D, d M Y H:i:s \G\M\T', time() + (60 * 60))); // 1 hour