您的位置:首页 > 运维架构 > Nginx

nginx 学习笔记

2016-01-15 17:29 561 查看
1. 安装# sudo apt-get install nginx1.1各个目录可执行文件目录: /usr/sbin配置文件目录: /etc/nginx/nginx.conf记录pid的文件: /run/nginx.pid (在配置文件中配置)1.2 运行# /usr/sbin/nginx1.3 停止# sudo kill 'cat /run/nginx.pid'源码安装的各个默认目录
nginx path prefix: "/usr/local/nginx"
nginx binary file: "/usr/local/nginx/sbin/nginx"
nginx configuration prefix: "/usr/local/nginx/conf"
nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
nginx pid file: "/usr/local/nginx/logs/nginx.pid"
nginx error log file: "/usr/local/nginx/logs/error.log"
nginx http access log file: "/usr/local/nginx/logs/access.log"
nginx http client request body temporary files: "client_body_temp"
nginx http proxy temporary files: "proxy_temp"
nginx http fastcgi temporary files: "fastcgi_temp"
nginx http uwsgi temporary files: "uwsgi_temp"
nginx http scgi temporary files: "scgi_temp"
2. 语法
http {
server {
listen 8080;
root /data/up1;
location / {
}
}

server {
location / {
proxy_pass http://localhost:8080/; }

location ~ \.(gif|jpg|png)$ {
root /data/images;
}
}
}
3. 信号
3.1 可执行文件信号格式: nginx -s signal信号:stop — fast shutdownquit — graceful shutdownreload — reloading the configuration filereopen — reopening the log files例如: nginx -s reload主线程接收到reload信号,检查配置文件的语法并执行新的配置文件。如果成功,将开启新的工作进程并发送消息给旧的工作进程,要求旧的进程退出。否则,主线程取消改变并继续使用旧的配置文件工作;旧的工作进程接收到退出命令,不再接受新的连接,继续服务当前请求,直到所有请求被服务后,退出。3.2 进程信号3.2.1主进程信号:TERM, INT fast shutdown 快速关闭QUIT graceful shutdown 从容关闭HUP changing configuration, keeping up with a changed time zone (only for FreeBSD and Linux), starting new worker processes with a new configuration, graceful shutdown of old worker processes 重载配置,用新配置开始新的工作进程,从容关闭旧的工作进程USR1 re-opening log files 重新打开日志文件USR2 upgrading an executable file 平滑升级可执行程序WINCH graceful shutdown of worker processes 从容关闭工作进程3.2.2 工作进程信号TERM, INT fast shutdown 快速关闭QUIT graceful shutdown 从容关闭USR1 re-opening log files 重新打来日志WINCH abnormal termination for debugging (requires debug_points to be enabled) 调试异常终止例如:kill -HUP 'cat /run/nginx.pid' 重载配置kill -USR1 'cat /run/nginx.pid' 替换日志文件kill -USR2 'cat /run/nginx.pid' 升级可执行文件4. 连接处理方式 (编译参数)select 标准方式
--with-select_module
--without-select_module
poll     标准方式 
--with-poll_module
[code]--without-poll_module
kqueue
高效方式 用在FreeBSD 4.1+, OpenBSD 2.9+, NetBSD 2.0, and Mac OS X.[/code]
epoll
高效方式 用在Linux 2.6+[/code]
/dev/poll
高效方式 用在Solaris 7 11/99+, HP/UX 11.22+ (eventport), IRIX 6.5.15+,and Tru64 UNIX 5.1A+.[/code]
eventport
高效方式 用在Solaris 10.[/code]
5.调试日志5.1 基本功能支持调试功能,需要编译时添加参数
./configure --with-debug ...
在配置文件中使用命令 error_log
error_log /path/to/log debug;
其中 debug为调试级别。nginx支持的级别分别为debug, info, notice, warn, error, crit,默认为crit,crit 记录的日志最少,而debug记录的日志最多。注:在server中重新定义日志但如果不指定调试级别,该server的调试功能将失效。例如:
error_log /path/to/log debug;

http {
server {
error_log /path/to/log;
...
5.2 针对某个客户端打印日志
error_log /path/to/log;

events {
debug_connection 192.168.1.1;
debug_connection 192.168.10.0/24;
}
5.3 设置循环内存缓冲区
error_log memory:32m debug;
使用debug级别打印日志到内存缓冲区,即使在高负载的情况下也不会明显影响性能。在这种情况下,可以使用gdb脚本提取日志,如下:
set $log = ngx_cycle->log

while $log->writer != ngx_log_memory_writer
set $log = $log->next
end

set $buf = (ngx_log_memory_buf_t *) $log->wdata
dump binary memory debug_log.txt $buf->start $buf->end
6.记录日志到syslogerror_log access_log 支持记录日志到syslog;配置参数:
server=address
定义一个syslog服务地址。该地址可以是域名或ip,端口号可选;也可以是UNIX域套接字路径(UNIX-domain socket path)(关于UNIX域套接字可见http://jishublog.iteye.com/blog/1945230 或自行百度),如果是UNIX域套接字路径,需要使用前缀"unix:"。如果端口号没有指定,使用UDP端口514。如果一个域名绑定了多个IP地址,使用第一个。
facility=string
指定日志消息的程序类型,默认“
local7
”,其他的还有:
kern
”, “[code]user
”,“
mail
”, “
daemon
”,“
auth
”, “
intern
”,“
lpr
”, “
news
”, “
uucp
”,“
clock
”, “
authpriv
”,“
ftp
”,
ntp
”, “
audit
”,“
alert
”, “
cron
”,“
local0
”..“
local7
”[/code]
详见RFC 3164
severity = string设置日志消息的严重级别,可使用的值按严重级别顺序递增为:debug, info, notice, warn, error (default), crit, alert, and emergtag=string为syslog 消息设置tag,默认为"nginx"nohostname禁用往syslog消息头添加"hostname"field.例子:
error_log syslog:server=192.168.1.1 debug;access_log syslog:server=unix:/var/log/nginx.sock,nohostname;
access_log syslog:server=[2001:db8::1]:12345,facility=local7,tag=nginx,severity=info combined;
7.配置文件中的单位size单位 bytes,kilobytes (suffixes k and K) or megabytes (suffixes m and M),例如“1024”, “8k”, “1m”.时间间隔:ms millisecondss seconds (default)m minutesh hoursd daysw weeksM months, 30 daysy years, 365 days8.命令行参数-?|-h —— 打印命令行参数帮助-c file —— 替换默认配置文件-g directives —— 设置全局配置文件命令 例如
nginx -g "pid /var/run/nginx.pid; worker_processes `sysctl -n hw.ncpu`;"
-p prefix —— 设置nginx路径前缀-q —— 配置测试时抑制错误信息-s signal
—— 设置信号,见3-t —— 测试配置文件-T —— 测试配置文件,转储配置文件到标准输出-v —— 打印版本信息-V —— 打印版本信息,编译器版本和配置参数9. 负载均衡由于Nginx的简单轻巧,如今越来越多的人采用Nginx当作Web服务器,Nginx除了可以当作web server之外,还可以利用Nginx来实现负载均衡,用Nginx来配置http(或https)的负载均衡也非常简单。Nginx的负载均衡支持以下几种方式:round-robin — 简单轮询(默认方式)least-connected — 最少活跃链接数(基本就是相对来说谁最闲就分配给谁)ip-hash — 根据IP进行哈希(请求来源的IP)举例说明,假设后台有3台服务器用户处理用户的请求,用Nginx来实现负载均衡:基本配置9.1.round-robin方式:
http {
upstream myapp1 {
server srv1.example.com;
server srv2.example.com;
server srv3.example.com;
}server {
listen 80;location / {
proxy_pass http://myapp1; }
}
}
当有用户请求时,Nginx会采用round-robin方式来分配各个请求到3台服务器上。9.2 least-connected方式:
http {
upstream myapp1 {
least_conn; #注意是least_conn不是least_connected
server srv1.example.com;
server srv2.example.com;
server srv3.example.com;
}server {
listen 80;location / {
proxy_pass http://myapp1; }
}
}
9.3 ip_hash
http {
upstream myapp1 {
ip_hash;
server srv1.example.com;
server srv2.example.com;
server srv3.example.com;
}server {
listen 80;location / {
proxy_pass http://myapp1; }
}
}
以上三种实现方式中,round_robin和least_conn不能保证同一用户的请求会发送到同一服务器上,如果你的服务中用到了Session的话,就需要考虑Session分布的问题。通常的解决办法是将Session统一存储,比如用Memcache或数据库存储。如果采用ip_hash可以将同一用户的请求发到同一台机器上。9.4 设置权重除了上面的配置以外,Nginx还支持给每台服务器设置权重:比如:
http {
upstream myapp1 {
ip_hash;
server srv1.example.com weight=3;
server srv2.example.com;
server srv3.example.com;
}server {
listen 80;location / {
proxy_pass http://myapp1; }
}
}
在srv1中加了一个权值3,这样分配给srv1的请求数将是其它机器的3倍,比如有5个请求,会分给srv13个,srv2和srv3各一个。least_conn和ip_hash也同样支持权重分配,配置方法和上面一样。9.5 错误检测Nginx在分配任务时会查看(被动)各台服务器的运行状态,如果分配给某台服务器的请求没有正确响应那么,Nginx会将该机器暂时标为不可用(请求仍会分配给其它服务器),可以通过max_fails参数(默认为1)来设置最大重试次数,用fail_timeout(默认为10s)来设置超时时间。如:
http {
upstream myapp1 {
ip_hash;
server srv1.example.com max_fails=2 fail_timeout=30s;
server srv2.example.com;
server srv3.example.com;
}server {
listen 80;location / {
proxy_pass http://myapp1; }
}
}
max_fails=2 fail_timeout=30s意思是在30秒内,连接该服务器连续2次失败,刚在接下来的30秒内,不再分配新的请求给这台机器(30秒之后新请求会重新尝试分配给该服务器)。如果设置max_fails=0,那么Nginx不检测这台机器的状况。如果想让某台机器下线,可以设置成down,如下:
http {
upstream myapp1 {
ip_hash;
server srv1.example.com max_fails=2 fail_timeout=30s;
server srv2.example.com down;
server srv3.example.com;
}server {
listen 80;location / {
proxy_pass http://myapp1; }
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: