運維小知識之nginx---nginx配置Jboss集羣負載均衡
背景
緊接着上一篇博客《運維小知識---CentOS6.5安裝nginx配置nginx sticky》安裝完成之後剩下的工作就是配置了,其實如果我們想要去做負載均衡session共享是一個繞不過去的問題,而解決session共享的方法有很多,我這裏介紹的事使用nginx sticky,方便易用。
具體配置
由於目前的項目中會出現什麼情況還不清楚,所以目前只是做了最簡單的配置,如下:
http {
include mime.types;
default_type application/octet-stream;
#Proxy_cache_path /usr/local/nginx/NginxTestImgLoglevels=1:2 keys_zone=cache_one:200minactive=1d max_size=30g;
#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 jboss6.2 { #服務器集羣名字
#server 172.16.21.13:8081 weight=1;#服務器配置 weight是權重的意思,權重越大,分配的概率越大。
sticky; #配置session共享
server xxx.xx.xx.25:80weight=1;
server xxx.xx.xx.26:80weight=1;
server xxx.xx.xx.27:80weight=1;
}
#當前的Nginx的配置
server {
listen 80;
server_name yyy.yy.yyy.121;
rewrite_log on;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
proxy_pass http://jboss6.2;
}
#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;
#}
}
目前只使用了一臺nginx服務器,如果其宕機了就無法怎麼辦這個問題也暫時沒有考慮(配置一個nginx集羣,當然這些都是後話了),筆者一直堅信最好的開發是按需而爲,不是炫技,不是過度設計。在技術這條路上,聽過,看過,最重要的是做過!
最後還想強調的一句話是——不解決session共享的負載均衡其實是耍流氓。筆者初步接觸nginx文中如有紕漏之處,還望您能不吝賜教!