nginx负载均衡详解
2016-05-14 23:47
826 查看
1:什么是负载均衡
负载平衡(Load balancing)是一种计算机网络技术,用来在多个计算机(计算机集群)、网络连接、CPU、磁盘驱动器或其他资源中分配负载,以达到最佳化资源使用、最大化吞吐率、最小化响应时间、同时避免过载的目的。2:应用场景
负载平衡主要应用于Web网站,大型的Internet Relay Chat网络,高流量的文件下载网站,NNTP(Network News Transfer Protocol)服务和DNS服务。现在负载平衡器也开始支持数据库服务,称之为数据库负载平衡器。3:存在的问题
负载均衡器需要处理的一个重要问题是:如何保存用户会话?如果会话信息保存在后台服务器,用户接下来的请求可能会被分配到不同的后台服务器,此时用户会话就无法继续。负载均衡器可以缓存用户会话,然后将用户请求分发到不同的后台服务器。但是这将带来负载均衡器的负载问题。解决这个问题有很多种方案,我尝试的是用memcached来保存session,多台TOMCAT服务器即可共享SESSION了。
修改$TOMCAT_HOME/conf/server.xml
<Context docBase="E:/java_codes/TestSession/WebContent" path="" reloadable="true" > <Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager" memcachedNodes="n1:localhost:11211" requestUriIgnorePattern=".*\.(png|gif|jpg|css|js)$" sessionBackupAsync="false" sessionBackupTimeout="100" transcoderFactoryClass="de.javakaffee.web.msm.serializer.javolution.JavolutionTranscoderFactory" copyCollectionsForSerialization="false" /> </Context>
这里的memcachedNodes是填写memcached节点,多个节点时可以以空隔分开,如:
n1:localhost:11211 n2:localhost:11212
sessionBackupTimeout的单位为分钟
E:/java_codes/TestSession/WebContent 替换成你的WEB目录
修改后重启两个TOMCAT即可,这个时候已经解决SESSION的共享问题.
参考文章:http://blog.csdn.net/u013628152/article/details/50485949
4:nginx安装及相关命令
(1)安装Nginx:yum install nginx(2)启动Nginx服务:service nginx start
(3)停止Nginx服务:service nginx stop
(4)查看Nginx运行状态:service nginx status
[root@iZ25knm9r1gZ ~]# service nginx status nginx (pid 16435) is running...
(5)检查Nginx配置文件:nginx -t
[root@iZ25knm9r1gZ ~]# nginx -t nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful
(6)服务运行中重新加载配置:nginx -s reload
(7)添加Nginx服务自启动:chkconfig nginx on
5:修改防火墙规则
修改Nginx所在主机的防火墙配置:vi /etc/sysconfig/iptables,将nginx使用的端口添加到允许列表中。例如:-A INPUT -m state –state NEW -m tcp -p tcp –dport 80 -j ACCEPT (表示允许80端口通过)
修改Tomcat所在主机的防火墙配置:vi /etc/sysconfig/iptables,将tomcat使用的端口添加到允许列表中。
例如:-A INPUT -m state –state NEW -m tcp -p tcp –dport 8080 -j ACCEPT (表示允许8080端口通过)
如果主机上有多个tomcat的话,则按此规则添加多条,修改对应的端口号即可。
保存后重启防火墙:service iptables restart
6:Tomcat负载均衡配置
Nginx启动时默认加载配置文件/etc/nginx/nginx.conf,而nginx.conf里会引用/etc/nginx/conf.d目录里的所有.conf文件。因此可以将自己定制的一些配置写到单独.conf文件里,只要文件放在/etc/nginx/conf.d这个目录里即可,方便维护。
创建tomcats.conf:vi /etc/nginx/conf.d/tomcats.conf,内容如下:
upstream tomcats { ip_hash; server 192.168.0.251:8080; server 192.168.0.251:8081; server 192.168.0.251:8082; }
修改default.conf:vi /etc/nginx/conf.d/default.conf,修改如下:
#注释原有的配置 #location / { # root /usr/share/nginx/html; # index index.html index.htm; #}
#新增配置默认将请求转发到tomcats.conf配置的upstream进行处理 location / { proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header REMOTE-HOST $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass http://tomcats; #与tomcats.conf里配置的upstream同名 }
保存后重新加载配置:nginx -s reload
7:静态资源分离配置
修改default.conf:vi /etc/nginx/conf.d/default.conf,添加如下配置:#所有js,css相关的静态资源文件的请求由Nginx处理 location ~.*\.(js|css)$ { root /opt/static-resources; #指定文件路径 expires 12h; #过期时间为12小时 } #所有图片等多媒体相关静态资源文件的请求由Nginx处理 location ~.*\.(html|jpg|jpeg|png|bmp|gif|ico|mp3|mid|wma|mp4|swf|flv|rar|zip|txt|doc|ppt|xls|pdf)$ { root /opt/static-resources; #指定文件路径 expires 7d; #过期时间为7天 }
8:修改SELinux安全规则
如果访问Nginx时出现502 Bad Gateway错误,则可能是Nginx主机上的SELinux限制了其使用http访问权限引起的,输入命令setsebool -P httpd_can_network_connect 1 开启权限即可。文件/etc/nginx/nginx.conf完整配置如下:
user nginx; worker_processes auto; error_log /var/log/nginx/error.log warn; pid /var/run/nginx.pid; worker_rlimit_nofile 100000; events { use epoll; multi_accept on; worker_connections 1024; } http { include /etc/nginx/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 /var/log/nginx/access.log main; sendfile on; server_tokens off; #tcp_nopush on; keepalive_timeout 65; gzip on; gzip_disable "msie6"; gzip_static on; gzip_proxied any; gzip_min_length 1000; gzip_comp_level 4; gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript; include /etc/nginx/conf.d/*.conf; }
文件/etc/nginx/conf.d/default.conf完整配置如下:
server { listen 80; server_name localhost; #charset koi8-r; #access_log /var/log/nginx/log/host.access.log main; #location / { # root /usr/share/nginx/html; # index index.html index.htm; #} location / { proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header REMOTE-HOST $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass http://web_servers; } location ~.*\.(js|css)$ { root /opt/static-resources; expires 12h; } location ~.*\.(html|jpg|jpeg|png|bmp|gif|ico|mp3|mid|wma|mp4|swf|flv|rar|zip|txt|doc|ppt|xls|pdf)$ { root /opt/static-resources; expires 7d; } #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 /usr/share/nginx/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; #} }
注意:如果执行命令时没有root权限,请在命令前面加上 sudo 执行。
相关文章推荐
- nginx代理指定目录
- 访问Nginx发生SSL connection error的一种情况
- Nginx+Naxsi部署专业级Web应用防火墙
- CentOS 6.2实战部署Nginx+MySQL+PHP
- nginx中http核心模块的配置指令2
- nginx中http核心模块的配置指令3
- nginx中http核心模块的配置指令4
- nginx中http的fastcgi模块的配置指令1
- 负载均衡技术沙龙1期(关于咱的图片)
- 负载均衡沙龙活动第二期现场问答汇集
- Nginx 学习笔记(一)
- 流量引导:网络世界的负载均衡解密
- 流量引导:网络世界的负载均衡解密
- 网站502与504错误分析
- 用zabbix监控nginx_status状态
- 艰难完成 nginx + puma 部署 rails 4的详细记录
- 把Lua编译进nginx步骤方法
- 浅谈sqlserver的负载均衡问题
- web 应用中常用的各种 cache详解