Nginx

Nginx 維護模式配置

  • June 24, 2020

如何在 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


}

你可以使用這樣的東西:

server {
   ...
   set $maintenance '';
   if ($maintenance) {
       rewrite ^ /maintenance/ last;
   }
   ...
   location /maintenance {
       internal;
       root /var/www;
   }
}

要啟用維護模式,請將$maintenance變數設置為任何非空字元串,例如

set $maintenance 1;

並重新載入(這是零停機操作)nginx。

引用自:https://serverfault.com/questions/1022745