2
0
mirror of https://github.com/tmiland/Nginx-Maintenance-Mode synced 2024-09-14 16:10:34 +02:00

Added Bonus

- Added error pages and example Scheduled maintenance.
This commit is contained in:
Tommy Miland 2019-04-02 16:27:45 +02:00
parent e731f1b4b3
commit 3a94438bdb
3 changed files with 107 additions and 11 deletions

View File

@ -21,10 +21,10 @@
## Easily toggle on or off maintenance mode with nginx
## Screenshots
### Screenshots
![screenshot](https://raw.githubusercontent.com/tmiland/Nginx-Maintenance-Mode/master/img/maintenance.png)
## Installation
### Installation
#### Download and execute the script:
@ -37,7 +37,7 @@ $ cp -rp /etc/nginx/html/server-error-pages/_site/maintenance-page.html /etc/ngi
$ chmod +x maintenance.sh
```
### Add to server directive
#### Add to server directive
```
server {
@ -48,24 +48,21 @@ include snippets/maintenance-page.conf;
}
```
### Check for errors
#### Check for errors
```bash
$ nginx -t
```
- Add to each virtual server in /etc/nginx/sites-available you want to enable maintenance mode on.
- Else, add to nginx.conf.
## Usage
#### Usage
```bash
$ ./maintenance.sh [hostname] [on/off]
```
*** Note: [hostname] must match the hostname in your.hostname.com.conf/nginx.conf ***
## E.G
```
server {
server_name your.hostname.com;
@ -76,16 +73,18 @@ server {
- This way you can use this with multiple virtual servers, and easily toggle on/off maintenance mode individiually for each site.
- Toggle off, deletes $server_name-maintenance-page_on.html
# Advanced usage
### Advanced usage
```bash
$ ./maintenance.sh my.hostname.com on
$ ./maintenance.sh my-other.hostname.com on
$ ./maintenance.sh hostname.com on
$ ./maintenance.sh my.hostname.com off
$ ./maintenance.sh my-other.hostname.com off
$ ./maintenance.sh hostname.com off
```
## Logic in maintenance-page.conf
#### Logic in maintenance-page.conf
```
@ -102,9 +101,69 @@ location @maintenance
```
- If your.hostname.com-maintenance-page_on.html exists, a error 503 is returned.
- If your.hostname.com-maintenance-page_on.html exists, error 503 is returned.
- If not, operation is back to normal.
## Added Bonus
#### Add support for custom error pages
*** This will add the error pages included in the repo ***
```bash
$ cp -rp ./error_pages.conf /etc/nginx/snippets/
$ cp -rp ./error_pages_content.conf /etc/nginx/snippets/
```
#### Add to server directive
*** Final configuration should look like this: ***
```
server {
## Nginx Maintenance Mode
include snippets/maintenance-page.conf;
## Custom Error Pages
include snippets/error_pages.conf;
}
```
## Scheduled maintenance
Example shell script:
Name the script, someting like: ```pg_backup_cron.sh```
```bash
#!/usr/bin/env bash
# Turn on maintenance mode
/path/to/Nginx-Maintenance-Mode/maintenance.sh hostname.com on
# Run database backups
/path/to/pgbackup/pg_backup.sh
# Turn off maintenance mode
/path/to/Nginx-Maintenance-Mode/maintenance.sh hostname.com off
exit 0
```
Add job to cron:
```
$ crontab -e
```
```
@daily bash /path/to/pgbackup/pg_backup_cron.sh > /dev/null 2>&1 #Automated PostgreSQL Backup on Linux
```
Used in this example: [pgbackup - Automated PostgreSQL Backup on Linux](https://github.com/tmiland/pgbackup)
## Why maintenance mode is important
- Helps your SEO rankings
As [recommended by Google](https://webmasters.googleblog.com/2011/01/how-to-deal-with-planned-site-downtime.html), Maintenace Mode is using the 503 service unavailable result code, which tells search engine crawlers that the downtime is temporary.
## Credits

30
error_pages.conf Normal file
View File

@ -0,0 +1,30 @@
include snippets/error_pages_content.conf;
location = /403-error.html {
root /etc/nginx/html/server-error-pages/_site;
internal;
}
location = /404-error.html {
root /etc/nginx/html/server-error-pages/_site;
internal;
}
location = /rate-limit-error.html { # 444-error
root /etc/nginx/html/server-error-pages/_site;
internal;
}
location = /500-error.html {
root /etc/nginx/html/server-error-pages/_site;
internal;
}
location = /502-error.html {
root /etc/nginx/html/server-error-pages/_site;
internal;
}
location = /503-error.html {
root /etc/nginx/html/server-error-pages/_site;
internal;
}
location = /504-error.html {
root /etc/nginx/html/server-error-pages/_site;
internal;
}

7
error_pages_content.conf Normal file
View File

@ -0,0 +1,7 @@
error_page 403 /403-error.html;
error_page 404 /404-error.html;
error_page 444 /rate-limit-error.html;
error_page 500 /500-error.html;
error_page 502 /502-error.html;
error_page 503 /503-error.html;
error_page 504 /504-error.html;