Voglio servire file HTML statici con NGINX, ma se il file è mancante, dovrebbe caricare un file PHP e PHP dovrebbe gestire il contenuto.Nginx per servire la pagina statica prima della dinamica
Ho provato diverse combinazioni di try_files
, ma non riesco a capirlo. Ho un'applicazione PHP fittizio che assomiglia a questo:
./
../
dynamic.php
index.php
static/
static/static.html
poi ho un piccolo codice PHP su indice come questo:
<?php
$path = $_SERVER['REQUEST_URI'];
$pattern = '/^\/(.*)\.html$/';
$matches = [];
$results = preg_match($pattern, $path, $matches);
if (count($matches) > 0) {
if ($matches[1] == "dynamic") {
require 'dynamic.php';
} else {
echo "Not found!";
}
} else {
echo "Index page!";
}
I risultati di navigazione per ogni pagina dovrebbe essere:
http://foo.bar/ - Loads index.php
http://foo.bar/static.html - Loads static/static.html
http://foo.bar/dynamic.html - Loads index.php & PHP requires dynamic.php
http://foo.bar/baz.html - Loads index.php with "not found" message
Questo è quello che ho ottenuto nel file di configurazione Nginx:
server {
listen 80;
server_name .foo.bar *.foo.bar;
access_log /var/log/nginx/foo.access.log;
error_log /var/log/nginx/foo.error.log;
root /var/www/foo;
index index.php;
location/{
# Trying with 'try_files' here. No success.
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm-foo.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
Ho cercato più volte ed evidentemente del tutto non riuscendo con questa linea:
try_files $uri $uri/static /index.php;
mi manca qualcosa. Aiuto?
non è forse possibile nel tuo caso per fare un po 'la riscrittura degli URL, ma gestire la pagina visualizzata con PHP per tutto il tempo? – AntoineB