Nginx 简介
1 WWW server : Apache Nginx Lighttpd Tomcat 'IBM WebSphere' IIS 'Google->GWS,GFE'
2 Nginx:是HTTP,SMTP,IMAP反向代理服务器,采用的网络I/O模型linux->epoll;FreeBSD->kqueue;Solaris 10->eventport.
特征:高并发,很稳定,各种资源消耗少.
其他特征:配置文件简单,支持Rewrite,Gzip,内建健康检查,支持热部署等。
Nginx 安装配置
1 依赖库 : zlib,openssl | libssl,pcre
Nginx 优化配置
1 主配置文件构成
......
events{
......
}
http{
......
server{
......
}
server{
......
}
......
}
2 虚拟主机配置文件例子
基于IP的虚拟主机的配置
ifconfig eth0:1 192.168.0.125 broadcast 192.1680.255 netmask 255.255.255.0 up
server{
listen IP:Port;
server_name IP;
location / {
index index.php index.html index.htm;
root /data/httpd/test
}
}
基于域名的虚拟主机的配置
server{
listen Port;
server_name domain_name1 domain_name2 *.domain.com;#支持多域名和通配符
location / {
index index.php index.html index.htm;
root /data/httpd/test
}
}
还有基于端口的虚拟主机,配置类似略;
关于日志的配置只有两条:
access_log, 设置日志路径,大小和格式
log_format, 设置日志格式
例 log_format:
log_format formatName [formatParam ...]
log_format timing '$remote_addr - $remote_user [$time_local] $request '
'upstream_response_time $upstream_response_time '
'msec $msec request_time $request_time';
例子 access_log:
access_log path [formatName [buffer=size]|off]
access_log /tmp/log/$server_name.log formatName buffer=1M
PS:open_log_file_cache 用于缓存日志路径的文件的描述符,语法
open_log_file_cache max=N [inactive=time] [min_uses=N] [valid=time] | off
open_log_file_cache max=1000 inactive=20s min_uses=2 valid=1m;
Nginx 自动列目录配置
在server->location中
autoindex on;
还有两个辅助功能指令
autoindex_exact_size [on|off]
autoindex_localtime [on|off]
Nginx 配置浏览器缓存
作用域http,server,location
expires [time|epoch|max|off] #通过输出header头实现(Expires和Cache-Control)
例子, 一些JS,CS和IMG常设置过期时间
location ~.*/.(gif|jpg|jpeg|png|swf)${
expires 30d;
}
location ~.*/.(js|css)?${
expires 1h;
}
Nginx PHP(FastCGI)安装配置优化
FastCGI的主要行为是:将多个CGI解释器进程保持在内存中已获得高性能。
Nginx HTTP 负载均衡和反向代理的配置与优化
常见的WEB负载均衡方法
1 用户手动选择方式 如华军
2 DNS轮询方式:对统一主机名添加多条A记录。
3 四/七曾负载均衡硬件|软件(Nginx 术语7层的软件负载均衡)
4 多线多地区智能DNS解析与混合负载均衡方式 如新浪网
Nginx 实现反向代理的负载均衡
主配置文件的主机池设置,可以配置多个
upstream machinePoolName1{
#ip_hash;
server IP:Port weight=N max_fails=N fail_timeout=time;
server IP1:Port down;
server domain_name;
server IP2:Port backup;
server unix:/tmp/var/backend;
}
对应每个主机池建立相应的虚拟主机做代理
server{
listen 80;
server_name your_domain_name your_domain_name1 your_domain_name2*;
#server_name also can be IP,Port and Unix Socket.
# for http:#your_domain_name/path/...
location /path/ {
proxy_pass http://machinePoolName1Message;
proxy_set_header ....;
access_log off;
}
# for all from http:#your_domain_name except http:#your_domain_name/message....
location / {
proxy_next_upstream http_502 http_504 error timeout invalid_header;
proxy_pass http:#machinePoolName1;
proxy_set_header Host your_domain_name;
.
.
}
access_log /data/log/your_domain_name.log;
}
******PS 在server{......}中可以用 proxy_pass , fastcgi_pass指令设置反向代理的upstream的服务器集群********
Nginx负载均衡反向代理实现动静态网页分离
Ngnix负载均衡服务器的双击热备:比较负载