nginx負(fù)載均衡的設(shè)定過程
前面我們對nginx負(fù)載均衡的安裝進(jìn)行了詳細(xì)的介紹,想必大家應(yīng)經(jīng)把系統(tǒng)平臺配置好了。那么接下來就是nginx負(fù)載均衡的配置講解了。在這里,我們要強(qiáng)調(diào)一下在配置中要注意進(jìn)程的連接數(shù)和事件問題。
nginx負(fù)載均衡配置
conf/nginx.conf文件:
- user www www; # 工作進(jìn)程的宿主
- worker_processes 8; # 工作進(jìn)程數(shù),一般跟CPU核數(shù)目相同
- #error_log logs/error.log; debug 模式
- error_log logs/error.log notice;
- #error_log logs/error.log info;
- pid logs/nginx.pid;
- gzip on; #打開gzip模式
- gzip_camp_level 5; #壓縮級別 1-9 ,1 最快,9最慢
- gzip_min_length 1100;
- gzip_buffers 4 8 k;
- worker_rlimit_nofile 51200;
- events {
- use epoll; # Linux下性能***的event
- worker_connections 51200; #每個(gè)進(jìn)程允許***的連接數(shù)}
- #access_log logs/access.log main; #日志文件名
- upstream tomcat{
- server 192.168.0.119:5050 down;
- server 192.168.0.117:5050 weight=1;
- server 192.168.0.142:5050 weight=1;}
- include /usr/local/nginx/conf/proxy.conf;
- location / {
- root html;
- index index.html index.htm;
- proxy_pass http://tomcat;}
- location /NginxStatus {
- stub_status on;
- access_log off;
- allow all;
- #auth_basic "status";
- #auth_basic_user_file conf/htpasswd;}
- # 靜態(tài)文件和圖片服務(wù)器時(shí)使用
- location ~ ^/images/{
- root /opt/webapp/images;}
- location ~ \.(htm|html|gif|jpg|jpeg|png|bmp|ico|css|js|txt)${
- root /opt/webapp;
- access_log off;
- expires 24h;}
- expires 60s;#30m 24h 1d max off
- location / {
- proxy_pass http://localhost:8080;
- proxy_set_header X-Real-IP $remote_addr;}
- location / {
- proxy_pass http://tomcat;
- proxy_set_header X-Real-IP $remote_addr;}
- #Nginx使用最簡單的平均分配規(guī)則給集群的節(jié)點(diǎn),達(dá)到負(fù)載均衡。若一個(gè)失效,或重新起效時(shí),Nginx會自己處理狀態(tài)的變化。
#p#proxy.conf 文件
- #!nginx (-)
- # proxy.conf
- proxy_redirect off;
- proxy_set_header Host $host;
- proxy_set_header X-Real-IP $remote_addr;
- proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
- client_max_body_size 10m;
- client_body_buffer_size 128k;
- proxy_connect_timeout 90;
- proxy_send_timeout 90;
- proxy_read_timeout 90;
- proxy_buffers 32 4k;
- #sbin/nginx -t
- #ulimit -SHn 51200
- #sbin/nginx
- #kill -HUP 'cat /usr/local/nginx/logs/nginx.pid'
- #重新加載新的配置文件
監(jiān)控:http://localhost/NginxStatus active connections :當(dāng)前正在處理的活動(dòng)連接數(shù)。server accepts handled requests:總共處理的n個(gè)連接,成功創(chuàng)建n次握手(證明中間沒有失敗的),總共處理了n個(gè)請求。
reading: 讀取到客戶端的Header信息數(shù)。
writing: 返回給客戶端的Header信息數(shù)。
waiting: 開啟keep-alive 情況下,該值等于active -(reading+writing),nginx已經(jīng)在處理完成正在等候下一次請求指令的駐留連接。
nginx負(fù)載均衡總結(jié):
一般可以對nginx的 worker_processes和worker_connections 進(jìn)行調(diào)整,來達(dá)到性能的調(diào)優(yōu)。