nginx搭建集群
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了nginx搭建集群,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含5325字,纯文字阅读大概需要8分钟。
内容图文
![nginx搭建集群](/upload/InfoBanner/zyjiaocheng/946/4159a75f63a14d3186f5879f951cfc18.jpg)
Nginx简介
Nginx (engine x) 是一个高性能的HTTP和反向代理web服务器,同时也提供了IMAP/POP3/SMTP服务。Nginx是由伊戈尔·赛索耶夫为俄罗斯访问量第二的Rambler.ru站点(俄文:Рамблер)开发的,第一个公开版本0.1.0发布于2004年10月4日。
Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,在BSD-like 协议下发行。其特点是占有内存少,并发能力强,事实上nginx的并发能力确实在同类型的网页服务器中表现较好,中国大陆使用nginx网站用户有:百度、京东、新浪、网易、腾讯、淘宝等。
nginx处理请求是异步非阻塞,事件驱动的,所以在高并发的情况下,依然有很好的性能。
非阻塞:把整个过程切换成小的任务,通过任务间协作完成。 由一个专门的线程来处理所有的 IO 事件,并负责分发。
事件驱动:通过事件的触发来调用,而不是异步监视。
反向代理简介
在计算机网络中,反向代理是代理服务器的一种。服务器根据客户端的请求,从其关系的一组或多组后端服务器(如Web服务器)上获取资源,然后再将这些资源返回给客户端,客户端只会得知反向代理的IP地址,而不知道在代理服务器后面的服务器簇的存在。
一句话总结,反向代理就是服务器找服务器,而不是客户端找服务器的前向代理。
NgInx的安装
- 从官网下载最新nginx-1.9.0.,下载地址为:http://nginx.org/en/download.html
- 安装nginx完毕以后,进入conf目录
3.找到nginx.conf配置文件
4.配置文件内容
#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; #nginx默认连接数 } 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; #动态服务器组 upstream liuyan{ ip_hash; server 127.0.0.1:8081 weight=1;#weight权重,数字越大被使用的几率越高。 server 127.0.0.1:8082 weight=1; #可以配置无限多个服务器..... } server { listen 80; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { proxy_pass http://liuyan; } #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; # } #} }
5、启动自己tomcat
访问地址:http://127.0.0.1/
nginx的配置文件介绍
events { worker_connections 1024; //nginx默认连接数 }
upstream test.top{//集群的名字
ip_hash;//nginx的策略之一
#server 192.168.1.63:8081 weight=1;//weight权重,数字越大被使用的几率越高。
#server 39.98.245.21:8080;
.
.
.//可以配置无限多个服务器
}
server {
listen 80;//nginx监听的端口
server_name localhost;
#location / {
#root html;
#index index.html index.htm;
#}
location / {
proxy_pass http://test.top;
proxy_redirect default;
}
}
Nginx的策略介绍
-
权重配置:
weight和请求数量成正比,主要用于上游服务器配置不均衡的情况。下面的配置中,192.168.10.2机器的请求量是192.168.10.1机器请求量的2倍。
upstream test.top {
server 192.168.10.1:8668 weight=5;
server 192.168.10.2:8668 weight=10;
}
-
ip_hash配置:
每一个请求按照请求的ip的hash结果分配。这样每一个请求固定落在一个上游服务器,能够解决ip会话在同一台服务器的问题。
upstream nodes {
ip_hash;
server 192.168.10.1:8668;
server 192.168.10.2:8668;
}
-
fair配置:
按上游服务器的响应时间来分配请求。响应时间短的优先分配。
upstream nodes {
server 192.168.10.1:8668;
server 192.168.10.2:8668;
fair;
}
-
url_hash配置:
按照访问的url的hash结果来分配请求,使每一个url定向到同一个上游服务器。注意:在upstream中加入hash语句。server语句中不能写入weight等其他的參数,hash_method是使用的hash算法。
upstream nodes {
server 192.168.10.1:8668;
server 192.168.10.2:8668;
hash $request_uri;
hash_method crc32;
}
-
upstream中常用的配置项:
down:表示当前的server不參与负载均衡。
weight:默觉得1,weight越大,负载的权重就越大。
max_fails :请求失败的次数默觉得1。
fail_timeout : max_fails次失败后,暂停请求此台服务器的时间。
backup: 其他全部的非backup机器down或者忙的时候,请求backup机器。所以这台机器压力会最轻。
upstream nodes {
ip_hash;
server 192.168.10.1:8668 down;
server 192.168.10.2:8668 weight=2;
server 192.168.10.3:8668;
server 192.168.10.4:8668 backup;
}
内容总结
以上是互联网集市为您收集整理的nginx搭建集群全部内容,希望文章能够帮你解决nginx搭建集群所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。