nginx集群学习总结

nginx集群学习总结:

1.nginx介绍:

(1)nginx是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP代理服务器。
(2)优点是稳定/丰富的功能集/示例配置文件/低系统资源消耗。
(3)官方测试版本nginx可以支撑5万并发链接。并且CPU,内存等资源消耗非常低且运行稳定。
(4)功能作为前端服务器,负载均衡,储存静态资源。

2.Tengine是nginx的加强版,封装版,淘宝开源。

(1)官网:http://tengine.taobao.org/
(2)动态模块加载支持,加入一个模块不需重新编译整个tengine。
(3)…….

3.了解整个大的网站高并发,高可用,负载均衡的整个框架。

4.tengine的安装:(为了安全的话可以添加用户/组等)

在CentOS7以下版本:
(1)安装依赖 yum -y install gcc openssl-devel pcre-devel zlib-devel
(2)编译三步走./configure \
–prefix=/opt/yiyele/soft/tengine-2.1.0/ \
–error-log-path=/var/log/nginx/error.log \
–http-log-path=/var/log/nginx/access.log \
–pid-path=/var/run/nginx/nginx.pid \
–lock-path=/var/lock/nginx.lock \
–with-http_ssl_module \
–with-http_flv_module \
–with-http_stub_status_module \
–with-http_gzip_static_module \
–http-client-body-temp-path=/var/tmp/nginx/client/ \
–http-proxy-temp-path=/var/tmp/nginx/proxy/ \
–http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ \
–http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \
–http-scgi-temp-path=/var/tmp/nginx/scgi \
–with-pcre
make && make install
(3)添加到启动服务,注意:一定要从windows复制粘贴过来
在/etc/init.d下面建立nginx文件,粘贴内容,修改路径。
(4)修改ngxin文件执行权限。
(5)配置chkconfig

在CentOS7版本中安装:
(1)(2)两步基本相同,但是可以不用配置后面的变量,直接设置–prefix就可以了
(3)如需要开机没有登陆情况下就能启动tengine就在/lib/systemd/system里设置vi tengine.service。基本内容如下:
[Unit]
Description=The nginx HTTP and reverse proxy server
After=syslog.target network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/bin/kill -s HUP MAINPIDExecStop=/bin/killsQUIT MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target

只需要改动目录就可以了。

系统用户登录系统后启动,就放在/usr/xxx/lib/systemd/system目录中。
(4)systemctl daemon-reload
systemctl start tengine.service。
如果出现错误可以使用systemctl status tengine.service。
如果设置自启动,则是systemctl enable tengine.service。
关闭自启动,则是systemctl disable tengine.service。

5.tengine的配置文件 nginx.conf介绍:(查看官方文档http://tengine.taobao.org/nginx_docs/cn/docs/

(1)文档简介

user nobody;

worker_processes 1; //进程数,基本配置和CPU总核数相等。

#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;                                                               //每个进程最大链接数。
}

# load modules compiled as Dynamic Shared Object (DSO)
#
#dso {
#    load ngx_http_fastcgi_module.so;                                                       //common gateway inferface 公共网络接口
#    load ngx_http_rewrite_module.so;                                                       //url 地址重写
#}

http {                                                                                      //支持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;
#    }
#}

}
(2)虚拟主机:
基于ip的虚拟主机, (一块主机绑定多个ip地址)
基于域名的虚拟主机(servername)
基于端口的虚拟主机(listen如果不写ip端口模式)

(3)location映射:
“普通 location ”的匹配规则是“最大前缀”,因此“普 通 location ”的确与 location 编辑顺序无关;但是“正则 location ”的匹配规则是“顺序 匹配,且只要匹配到第一个就停止后面的匹配”;
“普通location ”与“正则 location ”之 间的匹配顺序是?先匹配普通 location ,再“考虑”匹配正则 location 。注意这里的“考 虑”是“可能”的意思,也就是说匹配完“普通 location ”后,
有的时候需要继续匹配“正 则 location ”,有的时候则不需要继续匹配“正则 location ”。两种情况下,不需要继续 匹配正则 location :
A.当普通 location 前面指定了“ ^~ ”,特别告诉 Nginx 本条普 通 location 一旦匹配上,则不需要继续正则匹配;
B.当普通location 恰好严格匹配上 ,不是最大前缀匹配,则不再继续匹配正则。

(4) IP访问控制
– location {
– deny IP /IP段
– deny 192.168.1.109;
– allow 192.168.1.0/24;192.168.0.0/16;192.0.0.0/8
– }

– 规则:按照顺序依次检测,直到匹配到第一条规则

(5)用户认证:
– location / {
– auth_basic “closed site”;
– auth_basic_user_file /var/users;
– }
a.首先安装httpd这个软件.
b.使用htpasswd这个命令,htpasswd =bcm /usr/yiyele zs 123456
c.在/usr/yiyele这个文件中确认加密后的用户名和密码。
d.在这个auth_basic_user_file选项后面添加加密后的密码文件名。
e.auth_basic,在用户认证中显示的信息。

(6)nginx访问状态监控:
– location /basic_status {
– stub_status on;
– }

tengine新增的状态监控:
upstream cluster1 {
# simple round-robin
server 192.168.0.1:80;
server 192.168.0.2:80;

    check interval=3000 rise=2 fall=5 timeout=1000 type=http;
    check_http_send "HEAD / HTTP/1.0\r\n\r\n";
    check_http_expect_alive http_2xx http_3xx;
}

location /status {
        check_status;

        access_log   off;
        allow SOME.IP.ADD.RESS;
        deny all;
    }

(7)反向代理:
而反向代理(Reverse Proxy)方式是指以代理服务器来接受internet上的连 接请求,然后将请求转发给内部网络上的服务器,
并将从服务器上得到的结果返回 给internet上请求连接的客户端,此时代理服务器对外就表现为一个反向代理服务器。

 upstream backend {
    server backend1.example.com       weight=5;
    server backend2.example.com:8080;
    server unix:/tmp/backend3;

server backup1.example.com:8080   backup;
server backup2.example.com:8080   backup;

}

server {
    location / {
        proxy_pass http://backend;
 }
}

注释:proxy_pass表示反向代理,可以直接指向代理的服务器url地址。
upstream模块功能是实现负载均衡。

(8)session一致和共享问题。
使用memcache数据库或者redis数据库。

(9)tengine的会话保持功能:
#insert + indirect模式:
upstream test {
session_sticky cookie=uid domain=www.xxx.com fallback=on path=/ mode=insert option=indirect;
server 127.0.0.1:8080;
}

server {
location / {
#在insert + indirect模式或者prefix模式下需要配置session_sticky_hide_cookie
#这种模式不会将保持会话使用的cookie传给后端服务,让保持会话的cookie对后端透明
session_sticky_hide_cookie upstream=test;
proxy_pass http://test;
}
}

(10)nginx+keepalived高可用
keepalived :利用虚拟ip+权重

发布了98 篇原创文章 · 获赞 337 · 访问量 48万+

猜你喜欢

转载自blog.csdn.net/yiyele/article/details/78574639
今日推荐