如何在 nginx 上打开维护模式,我使用 Nginx 托管多个 Angular 应用。它们都在自己的容器中运行。维护页面位于 /var/www/maintenance/。我当前的配置如下
server {
listen 80;
listen 443 ssl;
server_name example.eu;
ssl_certificate /etc/letsencrypt/live/example.eu-0001/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/example.eu-0001/privkey.pem; # managed by Certbot
# user ssl certificate check
ssl_client_certificate /home/user/CA/certs/users/klient.crt;
#ssl_crl /home/user/CA/crl/ca.crl;
ssl_verify_client on;
access_log /var/log/nginx/graylog_access.log;
error_log /var/log/nginx/graylog_error.log;
location /abc/ {
root /var/webapps/;
}
location = / {
proxy_pass http://localhost:5000/abcd;
}
location /abcd {
rewrite ^/abcd(/css/|/js/|/img/)(.*)$ $1$2 break;
proxy_pass http://localhost:5000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection keep-alive;
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
proxy_send_timeout 3600;
proxy_read_timeout 3600s;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header X-Forwarded-Proto "https";
}
location /efgh/ {
rewrite ^/efgh(/css/|/js/|/img/)(.*)$ $1$2 break;
proxy_pass http://localhost:5005;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection keep-alive;
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
proxy_send_timeout 3600;
proxy_read_timeout 3600s;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header X-Forwarded-Proto "https";
}
server {
if ($host = example.eu) {
return 301 https://$host$request_uri;
} # managed by Certbot
listen 80;
server_name example.eu;
return 404; # managed by Certbot
}
答案1
您可以使用类似这样的方法:
server {
...
set $maintenance '';
if ($maintenance) {
rewrite ^ /maintenance/ last;
}
...
location /maintenance {
internal;
root /var/www;
}
}
要启用维护模式,请将$maintenance
变量设置为任何非空字符串,例如
set $maintenance 1;
并重新加载(它是零停机时间操作)nginx。
答案2
如果维护页面只有 HTML
没有资产文件(CSS、JS、...)
location = @maintenance {
root html/;
# `break`: use this `location` block
rewrite ^ "/maintenance.html" break;
}
location / {
error_page 503 @maintenance;
return 503;
# ...
}
对于具有资产文件(CSS / JS / ...)的维护页面
其流程如下:
- 如果 URI
/
是return 503
- 内部重定向至
/maintenance.html
- 内部重定向至
- 如果在
maintenance
目录中找到 URI(针对资产),则返回状态为 200 的文件 - 所有其他请求,重定向至
/
# for handle maintenance web page
server {
listen unix:/var/run/maintenance.nginx.sock;
# for Windows
# listen 8000;
location @index {
# use redirect because if URI has sub-folder ($uri == /path/path/),
# the relative path for asset file (CSS, JS, ...) will be incorrect
rewrite ^ / redirect;
}
location = / {
# maintenance page URI
error_page 503 /maintenance.html;
return 503;
}
# use RegExp because it has higher priority
location / {
root html/maintenance/;
# redirect all URI to index (503) page (except asset URI),
# asset URI still return status 200
try_files $uri @index;
}
}
# original server block
server {
listen 80;
location / {
proxy_pass http://unix:/var/run/maintenance.nginx.sock:;
# for Windows
# proxy_pass http://127.0.0.1:8000;
}
# ...
}
- 维护时,
maintenance.conf
您可以将配置放入其中。include maintenance.conf
如果你需要绕过某些 IP 地址
更多详情这里
参考:Nginx 维护