Инструкция Своя страница для ошибки 404 в nginx


akhomlyuk

Ищу модераторов
Администратор
Сообщения
748
Симпатии
370
#1
404.png
Создадим свою страницу отдаваемую веб сервером при ошибке 404 (Not Found)
Директива error_page 404 используется в контексте http, server, location, if в location
Синтаксис error_page 404 ... [=[ответ]] uri;
Пример конфига для сайта, добавляю в контекст
server {
....
}
Bash:
nano /etc/nginx/conf.d/myopenweb.ru.conf[code]
[MOW=nginx]
server {
        listen 80 default_server;
        listen [::]:80 default_server;
        server_name _;
        return 301 https://$host$request_uri;
}
server {
        listen 443 http2 ssl;
        server_name myopenweb.ru www.myopenweb.ru;

 ssl_certificate /etc/nginx/ssl/myopenweb.ru.crt;
 ssl_certificate_key /etc/nginx/ssl/myopenweb.ru.key;

        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
        ssl_prefer_server_ciphers on;
        ssl_ciphers "EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH";
        ssl_ecdh_curve secp384r1;
        ssl_session_cache shared:SSL:10m;
        ssl_session_tickets off;
        ssl_stapling on;
        ssl_stapling_verify on;
        resolver 8.8.8.8 8.8.4.4 77.88.8.8 valid=300s;
        resolver_timeout 5s;
        add_header Strict-Transport-Security "max-age=63072000; includeSubdomains";
        add_header X-Frame-Options DENY;
        add_header X-Content-Type-Options nosniff;

        ssl_dhparam /etc/ssl/certs/dhparam.pem;

        location ~ /.well-known {
                allow all;
        }

        root /var/www/html/myopenweb.ru;
        index index.php index.html index.htm;

        location / {
        try_files $uri $uri/ /index.php?$uri&$args;
        index index.php index.html;
        }

        location /internal_data/ {
        internal;
        }
        location /library/ {
        internal;
        }
                    location ~ \.php$ {
        try_files $uri =404;

        fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;

        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param SCRIPT_NAME $fastcgi_script_name;
        fastcgi_buffer_size 128k;
        fastcgi_buffers 256 16k;
        fastcgi_busy_buffers_size 256k;
        fastcgi_temp_file_write_size 256k;
        fastcgi_read_timeout 600;
        include fastcgi_params;
        fastcgi_cache MYAPP;
        fastcgi_cache_valid 200 60m;
   }
error_page 404 /404error.html;
        location = /404error.html {
                root /usr/share/nginx/html;
                internal;
        }

}[code]
Далее создадим саму HTML страницу
[code=bash]nano /usr/share/nginx/html/404error.html[code]
[MOW=html]<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>404 Page Not Found</title>
</head>
<body>
<br><br>
<center>
<img src="https://myopenweb.ru/data/attachments/0/290-6e1720fd4fd6b0a3801f48c66f4cb4b0.jpg" />
<h1>Страница не найдена(404)</h1>
</center>
</body>
</html>
[code]
Перезапускаем nginx и проверяем
[code=bash]systemctl restart nginx[code]
[MEDIA=youtube]oLrCNoDgi9E[/MEDIA]