1、前言
在上一章记录了nginx的安装以后,这一章简单记录一下nginx的常用命令、配置文件结构、限流操作、常用负载均衡的方法。
nginx有一个master进程和多个worker进程。master进程主要负责读取和评估配置,并维护worker进程。worker进程负责实际的请求处理。worker进程的数量在配置文件中定义,可以指定一个固定值,也可以根据可用CPU内核的数量自动调整。nginx及其模块的工作方式由配置文件决定。默认情况下,配置文件的名字叫nginx.conf,并且所在位置是/usr/local/nginx/conf
2、启动、停止、重新加载
启动nginx服务;进入/usr/local/nginx/sbin文件下执行./nginx命令
查看帮助命令;执行./nginx -h查看nginx帮助命令
停止nginx命令;./nginx -s quit停止当前用户的nginx服务(执行这个命令的用户必须与启动nginx的用户相同)
重新加载nginx命令;./nginx -s reload 在无需重启nginx服务的基础上热加载nginx.conf配置文件。
一旦master进程收到重新加载配置的信号以后,它检查配置文件的语法,并尝试应用配置文件中提供的配置。如果成功的话,master进程会启动一个新的worker进程并且发送消息给旧的worker进程请求它们立即shut down。否则,master进程回滚本次更改,继续用旧的配置工作。当旧的worker进程收到一个shut down的命令时,它停止接受新的连接,并继续服务当前请求,直到它收到的所有请求都处理完成。此后,旧的worker进程就退出了。
查看正在运行的nginx进程;ps -ef | grep nginx、ps aux | grep nginx
3、配载文件结构
nginx由配置文件中的指令所控制的模块组成。指令分为简单指令和块指令。简单指令由名称和参数组成,它们之间用空格隔开,以分号(;)结束。块指令的结构与简单指令相同,但它不是以分号结尾,而是一组由大括号({ 和 })包围的附加指令。如果一个block指令可以在大括号中包含其他指令,那么它被成为上下文(例如:events, http, server 和 location)。events 和 http 指令在main上下文下,server指令在http中,location在server中。默认的配置文件nginx.conf如下:
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
4、限流操作
ngx_http_limit_req_module
模块(0.7.21)用于限制每一个定义的密钥的请求的处理速率,特别是从一个单一的IP地址的请求的处理速率。使用“漏斗”方法完成限制。设置共享内存区域和请求的最大突发大小。如果请求速率超过为区域配置的速率,则会延迟其处理,以便以定义的速率处理请求。过多的请求将被延迟,直到其数量超过最大突发大小为止,在这种情况下,该请求将因错误而终止 。默认情况下,最大突发大小等于零。例如,指令
“one”这个区域维护的内存是10M,并且这个区域的平均请求处理速率不能超过每秒1个请求。
平均每秒不允许超过1个请求,突发不超过5个请求。
在请求受到限制时延迟过多的请求,则应使用参数nodelay
以上配置将限制来自单个IP地址的请求的处理速率,同时限制虚拟服务器的请求处理速率;
如果请求速率过高将会返回错误页面,如下:
5、负载均衡常用方法
1、默认的负载均衡方式
上例中,同一个应用运行了3个实例。默认的负载均衡策略是循环。
2、最少连接负载均衡
另一个负载平衡原则是最少连接的。在某些请求需要更长的时间才能完成的情况下,最少连接允许更公平地控制应用程序实例上的负载。使用连接最少的负载平衡,nginx将尽量不让繁忙的应用服务器超载过多的请求,而是将新请求分发到不那么繁忙的服务器;
3、session持久化负载均衡
使用循环或最少连接负载平衡,每个后续客户机的请求都可能被分发到不同的服务器。不能保证同一个客户端总是指向同一个服务器。使用IP-hash,客户端的IP地址用作哈希key,以确定应该为客户端请求选择服务器组中的哪个服务器。此方法确保来自同一客户端的请求总是指向同一服务器,除非该服务器不可用;
4、带权重的负载均衡
还可以通过使用服务器权值进一步影响nginx的负载平衡算法,权重越高,访问的次数越多。
这块知识的参考文档:http://nginx.org/en/docs/http/load_balancing.html