2015-11-19 21 views
7

Ho un problema con expires intestazioni su file JavaScript che vengono generati da PHP ..nginx con scade il file JavaScript (generati dinamicamente da PHP)

Il sito ha due tipi di file JavaScript. Una parte è costituita da file javascript statici e una parte è generata dinamicamente da PHP.

conf senza scade intestazioni

Qui non expires intestazioni vengono aggiunte ai file .js (Tutti i file ritornano 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 con scade intestazioni

Quando si aggiunge una posizione per .js file quindi tutti i file generati dinamicamente restituiscono 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"; 
} 

Come gestire sia i file statici che dinamici generati .js con intestazioni expires?

file javascript Tutti genereated dinamicamente sono chiamati *-php.js

struttura File

/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 instradamento

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

se si crea js con php perché non si aggiunge l'intestazione con le funzioni di php? – Kiyan

+0

Ho aggiornato la mia domanda – clarkk

risposta

6

Per nginx, PHP non è mai Javascript. Nginx non può distinguere tra PHP che rende html e PHP che esegue il rendering di JavaScript (correggimi se ho torto).

Così la strada da percorrere sarebbe sia per l'installazione in una cartella separata con i file PHP che generano tutti i JS (codice non è testato!):

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

... o inviare l'intestazione con PHP stesso:

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

Ma come faccio a mettere "expires" su file .js? Il problema è che quando aggiungo il percorso per i file .js tutti i file .js generati dinamicamente restituiscono '404'. Vedi la mia domanda nel secondo conf. – clarkk

+0

O stai dicendo che tutti i file js generati da php dovrebbero essere chiamati 'dynamic_js.php' invece di' dynamic_js-php.js' – clarkk

+0

Sì, il secondo commento. Sarebbe anche meglio generare le tue risorse tramite un lavoro di build, quindi hai uno script PHP che genera file .js statici. – DanFromGermany

Problemi correlati