carstenkl
Goto Top

Custom-Wartungsseite bei nginx

Hallo,

wir nutzen einen nginx als Reverse-Proxy vor einem Applikationsserver, auf dem ein Onlineshop läuft.
Wir würden jetzt gerne - für größere Updates - einen Wartungsmodus aktivieren, der dazu führt, dass alle User eine Wartungsseite sehen, die direkt auf dem nginx liegt. Nur für einige IPs soll hier eine Ausnahme definiert sein, damit wir das Update durchführen und anschließend testen können.

Soweit funktioniert dies auch, wenn ich mit "set $wartung 1;" den Wartungsmodus aktiviere, allerdings kommt dann die Standard nginx 503 Fehlerseite, nicht meine Customseite "maintenance.html".

Wenn der Wartungsmodus deaktiviert ist, kann ich die Fehlerseite problemlos im Browser aufrufen (https://<meineDomain>/maintenance.html)
Hat jemand eine Idee, was ich falsch mache?
Er wirft ja den 503, aber es scheint als würde "error_page 503 /maintenance.html;" keine Wirkung zeigen.


set $wartung 1;

if ($remote_addr = 234.234.234.234) {
set $wartung 0;
}

if ($remote_addr = 123.123.123.123 ) {
set $wartung 0;
}

if ($wartung = 1) {
return 503;
}
error_page 503 /maintenance.html;
location ~ /maintenance.html$ {
root /srv/www/html/;
}


Und hier mal die gesamte Site-Config.
roxy_cache_path /srv/nginx/cache/meineDomain-live levels=1:2 keys_zone=meineDomain_cache:256m inactive=120d max_size=25G;

map $request_method $purge_method {
PURGE 1;
default 0;
}

map $cookie_countrygroupid $cookie_cache_key {
     "~([0-9]+)"                 $1;  
     default                     '1';  
}


server {
    listen 80;
    server_name meineDomain.de www.meineDomain.de;
    #return 301 https:{{comment_single_line_double_slash:0}}
         location ~ /api {
            proxy_cache off;
            proxy_ignore_headers "Set-Cookie";  
            proxy_pass          http://appserver;
        }
        location / {
            return 301 https://www.meineDomain.de$request_uri;
        }


}




server {
    listen 443 ssl;
    server_name meineDomain.de;

    ## SSL Zertifikate
    #ssl_certificate /srv/ssl_remote/live/meineDomain.de/fullchain.pem;
    #ssl_certificate_key /srv/ssl_remote/live/meineDomain.de/privkey.pem;

    ssl_certificate /srv/ssl/meineDomain/meineDomain.de.bundle.pem;
    ssl_certificate_key /srv/ssl/meineDomain/meineDomain.de.key.pem;


    ssl_dhparam /srv/ssl/dhparam.pem;
    ssl_session_timeout 5m;
    ssl_prefer_server_ciphers on;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # not possible to do exclusive

    return 301 https://www.meineDomain.de$request_uri;
}

server {
    status_zone meineDomain_desktop;
    listen 443 ssl;
    server_name www.meineDomain.de;

    access_log /var/log/troubleshoot.log troubleshoot;
    #access_log /var/log/nginx/meineDomain.access.log shop;
    error_log /var/log/nginx/meineDomain.error.log debug;
    proxy_cache                 meineDomain_cache;
    rewrite_log on;

    set $cache_key "$request_uri";  
    proxy_cache_key "$cookie_cache_key$request_uri";  


    # Caching deaktivieren
    set $no_cache "";  
        #if ($http_cookie ~* "akosha_session*"){ 
        #set $no_cache 1;
        #}

    # NoCache URLs
    if ($request_uri ~* "(/admin.*|/brand.*|/user.*|/login.*|/de/favoriten.pdf)") {  
        set $no_cache 1;
    }

    # Wartungsmodus START
    set $wartung 0;

    if ($remote_addr = 127.0.0.1) {
        set $wartung 0;
    }

    if ($remote_addr = a.a.a.a ) {
        set $wartung 0;
    }

    if ($wartung = 1) {
        return 503;
    }
    error_page 503 /maintenance.html;
    location ~ /maintenance.html$ {
        root /srv/www/meineDomain.de/;
    }

    # Wartungsmodus ENDE

  
    # temorär proxycache deaktivieren
    proxy_no_cache $no_cache;


    ## SSL Zertifikate
    ssl_certificate /srv/ssl/meineDomain/meineDomain.de.bundle.pem;
    ssl_certificate_key /srv/ssl/meineDomain/meineDomain.de.key.pem;


    ssl_dhparam /srv/ssl/dhparam.pem;
    ssl_session_timeout 5m;
    ssl_prefer_server_ciphers on;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # not possible to do exclusive


    ## Block some UserAgents
    if ($badagent) {
        return 403;
    }

    ## Block SQL injections
    set $block_sql_injections 0;
    if ($query_string ~ "union.*select.*\(") {  
        set $block_sql_injections 1;
    }
    if ($query_string ~ "union.*all.*select.*") {  
        set $block_sql_injections 1;
    }
    if ($query_string ~ "concat.*\(") {  
        set $block_sql_injections 1;
    }
    if ($block_sql_injections = 1) {
        return 403;
    }

    ## Block GEO-IP
    if ($allowed_country = no) {
       return 444;
    }

     location ~ /robots.txt$ {
        root /srv/www/meineDomain.de/;
     }
     location ~ /web-sitemap.xml$ {
        root /srv/www/meineDomain.de/;
     }



    # jobs
    location ~ /jobs$ {
       return 301 https://www.meineDomain.de/jobs.html;
    }

    # Adminbereich
    location ~ /admin {
            # Access by IP
            satisfy  any;
            allow 10.0.2.0/24;
            allow a.a.a.a/32;
            deny   all;

            # Access by Passwort
            auth_basic "Restricted Content";  
            auth_basic_user_file /etc/nginx/.htpasswd;

        proxy_cache off;
        proxy_pass              http://appserver;
    }


   # Adminbereich
    location ~ /custom/admin {
            # Access by IP
            satisfy  any;
            allow 10.0.2.0/24;
            allow a.a.a.a/32;
            deny   all;

            # Access by Passwort
            auth_basic "Restricted Content";  
            auth_basic_user_file /etc/nginx/.htpasswd;

        proxy_cache off;
        proxy_pass              http://appserver;
    }


    # Startseite
    location ~ /$ {
        #proxy_cache off;
        proxy_ignore_headers "Set-Cookie";  
        proxy_pass              http://appserver;
        health_check;
        proxy_cache_purge $purge_method;
    }

    # Cachen
    location ~* \.(html)$ {
        #proxy_cache off;
        proxy_ignore_headers "Set-Cookie";  
        proxy_pass              http://appserver;
        health_check;
        proxy_cache_purge $purge_method;
     }


    # Cachen
    location ~ /blog {
        #proxy_cache off;
        proxy_ignore_headers "Set-Cookie";  
        proxy_pass              http://appserver;
        health_check;
        proxy_cache_purge $purge_method;
     }

    # Cachen
    location ~* \.(webp|gif|jpg|png|pdf)$ {
        expires 365d;
        proxy_ignore_headers "Set-Cookie";  
        proxy_pass              http://appserver;
        health_check;
        proxy_cache_purge $purge_method;
     }

    # Cachen
    location ~* \.(js|css|woff|woff2|otf|ttf|eot|svg|ico|map|glyphs)$ {
        expires 365d;
        proxy_ignore_headers "Set-Cookie";  
        proxy_pass              http://appserver;
        health_check;
        proxy_cache_purge $purge_method;
     }

    # nicht cachen (Warenkorb usw)
    location ~* \.(cfc|cfm|htm|gz|xml|bin|txt|json)$ {
        proxy_cache off;
        proxy_pass              http://appserver;
        health_check;
     }

     location ~ /api {
        proxy_cache off;
        proxy_ignore_headers "Set-Cookie";  
        proxy_pass          http://appserver;
     }

    location ~ /admin$ {
        rewrite ^/admin$ /admin/ break;
        proxy_pass          http://appserver;
    }


  

}

Content-Key: 3790272700

Url: https://administrator.de/contentid/3790272700

Printed on: May 5, 2024 at 21:05 o'clock

Member: ludaku
ludaku Aug 30, 2022 at 09:20:44 (UTC)
Goto Top
Ich weiss nicht ob das bei der Verarbeitung bei nginx eine Rolle spielt, aber hast du schon mal probiert
error_page 503 /maintenance.html;
vorher zu setzen? z.B. so:
error_page 503 /maintenance.html;
location ~ /maintenance.html$ {
  root /srv/www/html/;
}

set $wartung 1;

if ($remote_addr = 234.234.234.234) {
  set $wartung 0;
}

if ($remote_addr = 123.123.123.123 ) {
  set $wartung 0;
}

if ($wartung = 1) {
  return 503;
}

Jenachdem wie nginx dies verarbeitet wird ja sonst durch return 503 die abarbeitung beendet, bevor er überhaupt die Definition der 503 Seite erreicht.
Member: CarstenKl
CarstenKl Aug 30, 2022 at 09:27:01 (UTC)
Goto Top
Danke erstmal für die Idee, ich habe es gerade nochmal ausprobiert, da ich unsicher war, ob ich es bereits getestet hatte. Das Verhalten ist leider unverändert (nginx restart natürlich gemacht).
Member: CarstenKl
Solution CarstenKl Aug 30, 2022 at 09:37:55 (UTC)
Goto Top
Ich habe die Lösung gefunden, auch wenn ich sie so nicht optimal finde.
Der Block
    if ($wartung = 1) {
        return 503;
    }

muss zwingend innerhalb eines location Blocks...also z.B. so.
    # Cachen
    location ~* \.(html)$ {
        if ($wartung = 1) {
           return 503;
        }
        #proxy_cache off;
        proxy_ignore_headers "Set-Cookie";  
        proxy_pass              http://appserver;
        health_check;
        proxy_cache_purge $purge_method;
     }

Der "Rest" kann oben stehen bleiben.

Komisch, ich hätte erwartet, dass man das auch außerhalb setzen kann, denn das tue ich ja auch für Geo-Blocking und dergleichen.

Falls jemand noch eine Alternative hat, bin da für alles offen.

Danke auf jeden Fall schon einmal!
Member: CarstenKl
CarstenKl Aug 30, 2022 updated at 09:50:34 (UTC)
Goto Top
Das hässliche If habe ich jetzt noch durch das Geo-Module ausgetauscht
# Wartungsmodus aktivieren?
# default = 1
geo $wartungsmodus  {
        default         1;
        127.0.0.1       0;
        1.2.3.4  0;    # User xy
        10.0.x.0/24     0; # internes Netz
}