需求背景
线上运营服务,由于安全检查要求,应用全面升级等场景,这些都需要停服务,这个时候就需要基于域名提供友好的停服说明页面。由于平台服务涉及的服务比较多,每个服务设计实现停服页面比较麻烦,升级和维护成本高。本文介绍的是为使用nginx反向代理的服务,提供全局的停服页面。
nginx简介
Nginx是一款高性能的 HTTP 和反向代理服务器,由俄罗斯人Igor Sysoev(伊戈尔·赛索耶夫)为俄罗斯网站Rambler.ru开发的,在Rambler.ru网站平稳的运行了四年,而且俄俄罗斯超过20%的虚拟主机平台采用Nginx作为反向代理服务器。
Nginx的优点
1.高并发量:根据官方给出的数据,能够支持高达 50,000 个并发连接数的响应
2.内存消耗少:处理静态文件,同样起web 服务,比apache 占用更少的内存及资源,所有它是轻量级的
3.简单稳定:配置简单,基本在一个conf文件中配置,性能比较稳定,可以7*24小时长时间不间断运行
4.模块化程度高:Nginx是高度模块化的设计,编写模块相对简单,包括 gzipping, byte ranges, chunked responses,以及 SSI-filter 等 filter,支持 SSL 和 TLSSNI。
5.支持Rwrite重写规则:能够根据域名、URL的不同, 将HTTP请求分发到不同的后端服务器群组。
6.低成本:Nginx可以做高并发的负载均衡,且Nginx是开源免费的,如果使用F5等硬件来做负载均衡,硬件成本比较高。
7.支持多系统:Nginx代码完全用C语言从头写成,已经移植到许多体系结构和操作系统,包括:Linux、FreeBSD、Solaris、Mac OS X、AIX以及Microsoft Windows,由于Nginx是免费开源的,可以在各系统上编译并使用。
Nginx的缺点
1.动态处理差:nginx处理静态文件好,耗费内存少,但是处理动态页面则很鸡肋,现在一般前端用nginx作为反向代理抗住压力,apache作为后端处理动态请求。
2.rewrite弱:虽然nginx支持rewrite功能,但是相比于Apache来说,Apache比nginx 的rewrite 强大。
详细配置
user nginx;#修改用户名为nginx,安全
worker_processes 6;#开启六个nginx进程,提高性能
worker_cpu_affinity auto;#nginx进程与cpu绑定,提高性能
worker_rlimit_nofile 65535;
events {
worker_connections 65535;
use epoll;
}
http {
include mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $upstream_addr $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /usun/logs/nginx/access.log main;
error_log /usun/logs/nginx/error.log;#开启错误日志,用于调试
sendfile on;#性能
etag off;#使用Last-Modified和Expires
tcp_nodelay on;#性能
tcp_nopush on;#性能
server_tokens off;#向客户隐藏nginx的版本
#keepalive_timeout 0;
keepalive_timeout 30;#降低长连接时间,性能
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
#gzip_http_version 1.0;
gzip_comp_level 5;#提高压缩级别,节约带宽
gzip_types text/plain application/javascript application/css text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;
gzip_vary off;
gzip_disable "MSIE [1-6]\.";
client_max_body_size 20M;
upstream frontHost {
server 192.168.21.4:8080 max_fails=2 fail_timeout=3s;#定义后端服务器不可访问时的失效策略,默认为3次,10s
#server 192.168.21.5:8080 max_fails=2 fail_timeout=3s;
}
server {
listen 80;
server_name yuanin.net;
add_header X-Cache $upstream_cache_status;#查看缓存情况
add_header X-Via nginx;
#charset koi8-r;
#使用rewrite实现全域重定向
location / {
rewrite ^ /repair/ redirect;
}
#使用nginx代理静态文件访问(静态文件放在/usun/page/repair/目录下如:index.html)
location /repair/ {
root /usun/page/;
index index.html;
}
#error_page 404 /404.html;#开启404页面
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
location /ngx_status {
stub_status on;
access_log off;
allow 219.143.147.82;#定义nginx status页面,用于监控nginx
allow 127.0.0.1;
deny all;
}
}
}
涉及知识点
- 使用rewrite实现全域重定向
https://www.cnblogs.com/brianzhu/p/8624703.html
https://www.cnblogs.com/tugenhua0707/p/10798762.html - 使用nginx代理静态文件访问
https://www.jianshu.com/p/6c37abcf2e3d
臭味相投的朋友们,我在这里:
猿in小站:http://www.yuanin.net
csdn博客:https://blog.csdn.net/jiabeis
简书:https://www.jianshu.com/u/4cb7d664ec4b
微信免费订阅号“猿in”
注意:本文归作者所有,未经作者允许,不得转载