首页 > 解决方案 > NGINX 关闭或重新加载时如何加载 HTML 模板?

问题描述

在构建应用程序时,我正在尝试找到一种方法来加载html显示“服务器关闭”或类似内容的方法。

现在,我每次构建后端和前端时,如果我访问该站点,就会有几秒钟的时间看到以下模板:

在此处输入图像描述

我想自定义该页面或显示不同的模板:Server Down at the momentBuilding

nginx.conf的是以下。我应该将 403.html 模板加载的位置放在哪里?:这需要在我认为的构建文件夹之外,因为 403 页面在构建时出现。

 server {  # [ASK]: is this what's causing the problem ? 
root /home/smiling/smiling-frontend/website/build; ## development build
index index.html;

server_name frontend.develop.smiling.be;  ## development domain


charset  utf-8;
gzip on;
gzip_vary on;
gzip_disable "msie6";
gzip_comp_level 6;
gzip_min_length 1100;
gzip_buffers 16 8k;
gzip_proxied any;
gzip_types
  text/plain
  text/css
  text/js
  text/xml
  text/javascript
  application/javascript
  application/x-javascript
  application/json
  application/xml
  application/xml+rss;

location / {
  try_files $uri $uri/ /index.html;
}

location ~* \.(?:jpg|jpeg|gif|png|ico|cur|gz|svg|svgz|mp4|ogg|ogv|webm|htc|svg|woff|woff2|ttf)\$ {
  expires 1M;
  access_log off;
  add_header Cache-Control "public";
}

location ~* \.(?:css|js)\$ {
  expires 7d;
  access_log off;
  add_header Cache-Control "public";
}
  
location ~ /\.well-known {
  allow all;
}

location ~ /\.ht {
  deny  all;
}

add_header Access-Control-Allow-Origin '*/';
add_header Access-Control-Allow-Headers 'origin, x-requested-with, content-type, accept, authorization';
add_header Access-Control-Allow-Methods 'GET, POST';



listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/backend.develop.smiling.be/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/backend.develop.smiling.be/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

}

标签: templatesnginx

解决方案


你的最后一句话有点不一致......你喜欢不做某事,但仍然想做同样的事情。


您可以定义自己的页面或字符串以在错误时提供服务:

error_page 403 /403.html;

location = /403.html {
    internal;
    return 403 "Server Down at the moment"; # <- this could also contain an HTML string if your nginx defaults to text/html as content type.
}

您还可以将403.html文件放在根文件夹中并跳过位置部分,以便在此处提供完整的 HTML 文件。


推荐阅读