2011-09-23 13 views
13

Ho impostato nginx 0.8.53 con alcuni host virtuali che funzionano come voglio. Tuttavia, a causa della "migliore corrispondenza" di nginx sugli host virtuali, ho bisogno di aggiungere un host predefinito per catturare tutte le richieste che non riguardano un host virtuale specifico. Vorrei che l'host predefinito restituisse una pagina 404 che non dichiarasse che sto eseguendo nginx 0.8.53.Come ottenere nginx per restituire sempre un 404 personalizzato per l'host predefinito

ho assunto questo dovrebbe essere qualcosa di simile:

# The default server. 
# 
server { 
    listen  80 default_server; 
    server_name everythingelse; 

    # Everything is a 404 
    location/{ 
     return 404; 
    } 
    error_page 404 /opt/local/html/404.html; 
} 

Ma questo ancora restituisce la pagina di default nginx 404 che ha il numero di versione .. Sembra che il 'ritorno 404' ignora il config 'error_page' ..

risposta

2

Spostare la direttiva error_page il conf per prima di chiamare il ritorno 404.

Questo dovrebbe funzionare:

# The default server. 
# 
server { 
    listen  80 default_server; 
    server_name everythingelse; 
    error_page 404 /error_docs/404.html; 

    # Everything is a 404 
    location/{ 
     return 404; 
    } 

    # Custom Error Page 
    location /error_docs { 
     alias /opt/local/html/; 
     log_not_found off; 
     access_log off; 
    } 
} 

Questo utilizzerà lo stesso personalizzato per tutti i siti (server). È necessario aggiungere la posizione dei documenti di errore.

http { 
error_page 404 /error_docs/404.html; 

... 

    # The default server. 
    # 
    server { 
     listen  80 default_server; 
     server_name everythingelse; 

     # Everything is a 404 
     location/{ 
      return 404; 
     } 

     # Custom Error Page 
     location /error_docs { 
      alias /opt/local/html/; 
      log_not_found off; 
      access_log off; 
     } 
    } 
} 
3

Pochissime direttive in nginx utilizzano un percorso di filesystem. Volete qualcosa di simile:

# The default server. 
server { 
    listen  80 default_server; 
    server_name everythingelse; 

    root /opt/local/html; 

    error_page 404 /404.html; 

    # Everything is a 404 
    location/{ 
    return 404; 
    } 

    # EDIT: You may need this to prevent return 404; recursion 
    location = /404.html { 
    internal; 
    } 
} 
+0

Il codice gli darà la pagina 404 di default che lui dice che non vuole (e già ottiene in ogni caso) e in secondo luogo , la direttiva error_page ti consente di specificare una pagina di errore personalizzata. In effetti, "error_page 404/404.html "significa semplicemente utilizzare il file 404.html (file 404 predefinito) nella cartella di installazione di nginx, che può anche essere" error_page 404 /path/to/any/other/404.html "In alternativa, è possibile modificare il file predefinito – Dayo

+1

La pagina 404 predefinita non è affatto un file, e error_page non * prende * un percorso del file system, ma reindirizza internamente al dato uri, quindi va alla ricerca di /404.html. Potrebbe essere necessario aggiungere location = /404.html {internal;} o simile per impedire la visualizzazione della pagina 404 predefinita, ora che ci penso, perché il reindirizzamento interno finirà di nuovo in posizione/e non sono sicuro di quale sia il ritorno 404 ; farà lì senza recursive_error_pages su; (che sarebbe comunque male). – kolbyjack

+0

"La pagina 404 predefinita non è affatto un file" non è corretta. Puoi trovarla nella cartella/usr/share/nginx/html/(su redhat - potrebbe essere diverso per gli altri). La direttiva error_page accetta un uri che può essere sotto il webroot o alias come necessario. – Dayo

22

Ecco quello che ho nel mio conf per farlo funzionare:

# The default server. 
server { 
    listen  80 default_server; 
    server_name everythingelse; 

    error_page 404 /404.html; 

    # Everything is a 404 
    location/{ 
    return 404; #return the code 404 
    } 

    # link the code to the file 
    location = /404.html { 
    #EDIT this line to make it match the folder where there is your errors page 
    #Dont forget to create 404.html in this folder 
    root /var/www/nginx/errors/; 
    } 
} 
Problemi correlati