10.CentOS7安装Tenginx
Tengine是由淘宝网发起的Web服务器项目.它在Nginx的基础上,针对大访问量网站的需求,添加了很多高级功能和特性.Tengine的性能和稳定性已经在大型的网站如淘宝网,天猫商城等得到了很好的检验.它的最终目标是打造一个高效,稳定,安全,易用的Web平台
优点
- 资源占用少
- 支持高并发
- 反向代理,负载均衡,缓存服务
- 支持异步网络i/o时间模型epoll
1.安装
1.下载最新的Tenginx
2.安装之前检查CentOS7的依赖
#对nginx源码进行编译依赖gcc环境
yum install gcc-c++
#CRE(Perl Compatible Regular Expressions) 是一个Perl库,包括 perl 兼容的正则表达式库.nginx 的 http模块使用pcre来解析正则表达式,pcre-devel 是使用 pcre 开发的一个二次开发库
yum install -y pcre pcre-devel
# nginx使用zlib对http包的内容进行 gzip
yum install -y zlib zlib-devel
#OpenSSL 是一个强大的安全套接字层密码库,囊括主要的密码算法,常用的密钥和证书封装管理功能及SSL协议,并提供丰富的应用程序供测试或其它目的使用.nginx 不仅支持 http 协议,还支持https(即在ssl协议上传输http)
yum install openssl openssl-devel
2.安装Tenginx
#解压Tenginx
tar -zxvf tengine-2.3.3.tar.gz
#检查是否安装了
cd tengine-2.3.3
#指定安装路径
./configure --prefix=/opt/nginx233
#编译并安装
make && make install 
2.配置文件详解
- conf 存放nginx所有配置文件的目录,主要nginx.conf
- html 存放nginx默认站点的目录,如index.html、error.html等
- logs 存放nginx默认日志的目录,如error.log access.log
- sbin 存放nginx主命令的目录,sbin/nginx
nginx.conf
#user  nobody;
#定义了nginx的工作进程数,以Cpu核数为准
worker_processes  auto;
#Nginx错误日志存放路径
error_log  logs/error.log;
#Nginx服务运行后产生的pid进程号
pid        logs/nginx.pid;
events {
    #每个worker进程支持的最大连接数
    worker_connections  1024;
}
http {
    #包含/etc/nginx/conf.d/目录下所有以.conf结尾的文件
    include       mime.types;
    default_type  application/octet-stream;
    
    # 打开此nginx的访问日志功能,即可查看日志
    #日志格式设定
    #$remote_addr与$http_x_forwarded_for用以记录客户端的ip地址;
    #$remote_user:用来记录客户端用户名称;
    #$time_local: 用来记录访问时间与时区;
    #$request: 用来记录请求的url与http协议;
    #$status: 用来记录请求状态;成功是200,
    #$body_bytes_sent :记录发送给客户端文件主体内容大小;
    #$http_referer:用来记录从那个页面链接访问过来的;
    #$http_user_agent:记录客户浏览器的相关信息;
    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/acess.log main;
    
    sendfile        on;
    keepalive_timeout  65;
    # nginx开启静态资源压缩,比如nginx返回磁盘的html文件特别大,里面包含了诸多的静态文件,极大提升网站访问
    gzip  on;
    #提供静态资源缓存功能,第一次访问过网页后,nginx能让静态资源缓存到浏览器上
    #虚拟主机代码块
    server {
        #网站端口
        listen       80;
        #网站域名
        server_name  localhost;
        #网站编码
        charset utf-8;
        #access_log  logs/host.access.log  main;
        #access_log  "pipe:rollback logs/host.access_log interval=1d baknum=7 maxsize=2G"  main;
        #域名匹配模块
        location / {
            root   /crm/;
            index  index.html index.htm;
        }
        #添加404页面优化,且是相对于location中的/crm/路径
        error_page 404 /404.html
        #添加505页面
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
    server {
    listen  85;
    server_name  localhost
    charset utf-8;
    location / {
            root   /crm85/;
            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;
    #    }
    #}
}
重启nginx
nginx -s reload
3.反方向代理
| 名称 | 含义 | 作用 | 
|---|---|---|
| 正向代理 | 为局域网客户端向外访问Internet服务 | 1.使用缓冲特性减少网络使用率 2.必须采取安全措施确保内网客户端通过它访问外部网站 | 
| 反向代理 | 为局域网服务器向外提供Internet服务 | 1.可以使用负载平衡提高客户访问量,还可以基于高级URL策略和管理技术对服务进行高质量管控 2.对外提供服务是透明的,客户端并不知道自己访问的是一个代理 | 
#user  nobody;
#定义了nginx的工作进程数,以Cpu核数为准
worker_processes  auto;
#Nginx错误日志存放路径
error_log  logs/error.log;
#Nginx服务运行后产生的pid进程号
pid        logs/nginx.pid;
events {
    #每个worker进程支持的最大连接数
    worker_connections  1024;
}
http {
    #包含/etc/nginx/conf.d/目录下所有以.conf结尾的文件
    include       mime.types;
    default_type  application/octet-stream;
    # 打开此nginx的访问日志功能,即可查看日志
    #日志格式设定
    #$remote_addr与$http_x_forwarded_for用以记录客户端的ip地址;
    #$remote_user:用来记录客户端用户名称;
    #$time_local: 用来记录访问时间与时区;
    #$request: 用来记录请求的url与http协议;
    #$status: 用来记录请求状态;成功是200,
    #$body_bytes_sent :记录发送给客户端文件主体内容大小;
    #$http_referer:用来记录从那个页面链接访问过来的;
    #$http_user_agent:记录客户浏览器的相关信息;
    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/acess.log main;
    
    sendfile        on;
    keepalive_timeout  65;
    # nginx开启静态资源压缩,比如nginx返回磁盘的html文件特别大,里面包含了诸多的静态文件,极大提升网站访问
    gzip  on;
    #提供静态资源缓存功能,第一次访问过网页后,nginx能让静态资源缓存到浏览器上
    #虚拟主机代码块
    server {
        #网站端口
        listen       80;
        #网站域名
        server_name  localhost;
        #网站编码
        charset utf-8;
        #access_log  logs/host.access.log  main;
        #access_log  "pipe:rollback logs/host.access_log interval=1d baknum=7 maxsize=2G"  main;
        #域名匹配模块
        location / {
            root   /crm/;
            #将80端口的访问转发给85端口
            proxy_pass  http://192.168.211.133:85;
            index  index.html index.htm;
        }
        #添加404页面优化,且是相对于location中的/crm/路径
        error_page 404 /404.html
        #添加505页面
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
    server {
    listen  85;
    server_name  localhost
    charset utf-8;
    location / {
            root   /crm85/;
            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.负载均衡
| 含义 | 特点 | 
|---|---|
| 将大量并发请求分别按照不同的策略转发给upstream虚拟服务池中多台服务器的过程 | 当一台服务器宕机之后,仍能保持系统可用 | 
1.upstream配置
http{
upstream django {
       server 10.0.0.10:8000;
       server 10.0.0.11:9000;
}
server{
    location / {
            root   html;
            index  index.html index.htm;
            proxy_pass http://django;
	}
}
}
2.upstream分配策略
| 算法 | 含义 | 
|---|---|
| 轮询 | (不做配置,默认轮询)按时间顺序逐一分配到不同的后端服务器 | 
| weight | 加权轮询,weight值越大,分配到的访问几率越高 | 
| ip_hash | 每个请求按访问IP的hash结果分配,这样来自同一IP的固定访问一个后端服务器 | 
| url_hash | 按照访问URL的hash结果来分配请求,是每个URL定向到同一个后端服务器 | 
| least_conn | 最少链接数,那个机器链接数少就分发 | 
weight权重(优先级)
upstream django {
       server 10.0.0.10:8000 weight=5;
       server 10.0.0.11:9000 weight=10;#这个节点访问比率是大于8000的
}
ip_hash配置,根据客户端ip哈希分配(这样每个访客固定访问一个后端服务器,不能和weight一起用weight 权重)
upstream django {
    ip_hash;
       server 10.0.0.10:8000;
       server 10.0.0.11:9000;
}
backup(在非backup机器繁忙或者宕机时,请求backup机器,因此机器默认压力最小)
upstream django {
       server 10.0.0.10:8000 weight=5;
       server 10.0.0.11:9000;
       server node.oldboy.com:8080 backup;
}
 
                    
                     
                    
                 
                    
                
 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号