配置apache和nginx的tomcat負載均衡
概述
本篇文章主要介紹apache和nginx的相關配置,tomcat的相關安裝配置我在前面有寫過一篇,詳細介紹通過兩種配置方法配置nginx。
tomcat配置參考:http://www.cnblogs.com/chenmh/p/5048893.html
apache配置
原始碼安裝
./configure --prefix=/usr/local/apache (安裝目錄) make make install
對於2.4以上版本的apache在進行原始碼安裝的時候有的機器會提示缺少部分外掛例如:apr、apr-util、pcre,需要先將這些外掛安裝好然後再安裝apache
YUM安裝
yum install httpd
配置tomcate負載均衡
進入安裝目錄cnf.d資料夾下面,建立一個.conf字尾的檔案
touch apa.conf
vim apa.cnf
Listen 8051 <VirtualHost *:8051> ServerAdmin root@localhost ServerName localhost ErrorLog "/etc/httpd/logs/app_error.log" CustomLog "/etc/httpd/logs/app_access.log" common ProxyPass/ balancer://cluster/ stickysession=JSESSIONID|jsessionid nofailover=On lbmethod=byrequests timeout=5 maxattempts=3 ProxyPassReverse / balancer://cluster/ ProxyRequests Off ProxyPreserveHost On <proxy balancer://cluster> #BalancerMember ajp://localhost:8009 route=tomcat_a BalancerMember http://localhost:8080/Front #BalancerMember ajp://localhost:8010 route=tomcat_b BalancerMember http://localhost:8081/Front </proxy> </VirtualHost>
配置檔案一開始配置了apache的埠8051,然後在最下面配置了連線tomcat的專案埠,我這裡的配置的apache和tomcat都在一臺伺服器上面分別使用了不同的埠,在tomcat的webapps路徑下面建立了一個Front專案,專案下面存放了一個test.jsp的測試頁面
cd /usr/local/tomcat1/webapps mkdir Front cd Front touch test.jsp vim test.jsp
<font color=red>testa</font><b>
同樣在tomcat2中也使用同樣的方法建立測試頁面,但是將testa改成testb
接下來確保8051埠被啟用,也可以關閉防火牆,tomcat1、tomcat2都已啟動,啟動方法參考前面我寫的關於tomcat的文章,確保httpd也以啟動,如果已經將httpd加入了啟動服務,
啟動http服務
service httpd start
檢視服務啟動狀態
service httpd status
接下來在瀏覽器中輸入:http://localhost:8051/test.jsp
如果重新整理連線頁面結果是在testa和testb之間切換,說明配置成功。
nginx配置
原始碼安裝
建立nginx使用者組 groupadd nginx 建立nginx使用者 useradd -g nginx -s /sbin/nologin nginx 安裝相關外掛 yum install –y make zlib-devel openssl-devel pcre-devel 解壓nginx安裝包 tar zxvf nginx-1.8.0.tar.gz 進入安裝包 cd nginx-1.8.0 安裝 ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_gzip_static_module --with-http_stub_status_module make && make install
單個檔案配置方法
建立測試頁面
cd /usr/local/tomcat1/webapps mkdir MFront cd MFront touch index.jsp vim index.jsp
<font color=red>MFronttesta</font><b>
tomcat2也同樣操作,將MFronttesta改成MFronttestb
配置檔案
cd /usr/local/nginx/conf vim nginx.conf
user nginx nginx; worker_processes 8; pid /usr/local/nginx/nginx.pid; worker_rlimit_nofile 102400; events { use epoll; worker_connections 102400; } http { include mime.types; default_type application/octet-stream; fastcgi_intercept_errors on; charset utf-8; server_names_hash_bucket_size 512; client_header_buffer_size 1024k; large_client_header_buffers 4 128k; client_max_body_size 300m; sendfile on; tcp_nopush on; keepalive_timeout 600; tcp_nodelay on; client_body_buffer_size 512k; proxy_connect_timeout 5; proxy_read_timeout 600; proxy_send_timeout 50; proxy_buffer_size 16k; proxy_buffers 4 64k; proxy_busy_buffers_size 128k; proxy_temp_file_write_size 128k; gzip on; gzip_min_length 1k; gzip_buffers 4 16k; gzip_http_version 1.1; gzip_comp_level 2; gzip_types text/plain application/x-javascript text/css application/xml; gzip_vary on; ###2012-12-19 change nginx logs log_format main '$http_x_forwarded_for - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" $request_time $remote_addr'; upstream Front { server 127.0.0.1:8080 weight=1 max_fails=2 fail_timeout=30s; server 127.0.0.1:8081 weight=1 max_fails=2 fail_timeout=30s; } upstream MFront { server 127.0.0.1:8080 weight=1 max_fails=2 fail_timeout=30s; server 127.0.0.1:8081 weight=1 max_fails=2 fail_timeout=30s; } ####chinaapp.sinaapp.com server { listen 80; server_name localhost; location /Front { proxy_next_upstream http_502 http_504 error timeout invalid_header; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass http://Front; expires 3d; } location /MFront { proxy_next_upstream http_502 http_504 error timeout invalid_header; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass http://MFront; expires 3d; } } }
注意標紅色的地方,配置檔案中我配置了兩個專案分別是Front和MFront,在下面定義server裡面的專案名稱一定要跟上面配置負載的姓名名稱保持一致否則會出錯。
多個配置檔案配置方法
對於需要配置多個專案的時候如果所有的資訊都配置在一個nginx配置檔案當中會導致配置檔案內容過長,不好檢視,下面就使用多個配置檔案的配置方法
cd /usr/local/nginx/conf 建立相關聯的配置檔案 touch gzip.conf proxy.conf host.conf web.conf
vim nginx.conf
user nginx nginx; worker_processes 4; # 工作程序數,為CPU的核心數或者兩倍 error_log logs/error.log crit; # debug|info|notice|warn|error|crit pid logs/nginx.pid; events { use epoll; #Linux最常用支援大併發的事件觸發機制 worker_connections 65535; } http { include mime.types; #設定mime型別,型別由mime.type檔案定義 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; #設定請求緩衝 server_names_hash_bucket_size 256; #增加,原為128 client_header_buffer_size 256k; #增加,原為32k large_client_header_buffers 4 256k; #增加,原為32k types_hash_max_size 2048; proxy_headers_hash_bucket_size 1024; proxy_headers_hash_max_size 512; #size limits client_max_body_size 50m; #允許客戶端請求的最大的單個檔案位元組數 client_header_timeout 3m; client_body_timeout 3m; send_timeout 3m; sendfile on; tcp_nopush on; keepalive_timeout 120; tcp_nodelay on; server_tokens off; #不顯示nginx版本資訊 # client_body_buffer_size 1024K; # client_header_buffer_size 128k; # client_max_body_size 512m; # large_client_header_buffers 8 128k; # client_body_timeout 10; # client_header_timeout 10; # keepalive_timeout 60; # send_timeout 10; limit_conn_zone $binary_remote_addr zone=perip:10m; #新增limit_zone,限制同一IP併發數 #fastcgi_intercept_errors on; #開啟錯誤頁面跳轉 include gzip.conf; #壓縮配置檔案 include proxy.conf; #proxy_cache引數配置檔案 include host.conf; #nginx虛擬主機包含檔案目錄 include web.conf; #後端WEB伺服器列表檔案 }
注意最後的include這4個相關配置檔案,其它的一些相關配置分別在這四個配置檔案中配置。
cat gzip.conf
#啟動預壓縮功能,對所有型別的檔案都有效 gzip_static on; #開啟nginx_static後,對於任何檔案都會先查詢是否有對應的gz檔案 #找不到預壓縮檔案,進行動態壓縮 gzip on; gzip_min_length 1k; #設定最小的壓縮值,單位為bytes.超過設定的min_length的值會進行壓縮,小於的不壓縮. gzip_comp_level 3; #壓縮等級設定,1-9,1是最小壓縮,速度也是最快的;9剛好相反,最大的壓縮,速度是最慢的,消耗的CPU資源也多 gzip_buffers 16 64k; #設定系統的快取大小,以儲存GZIP壓縮結果的資料流,它可以避免nginx頻煩向系統申請壓縮空間大小 gzip_types text/plain application/x-javascript text/css text/javascript; #關於gzip_types,如果你想讓圖片也開啟gzip壓縮,那麼用以下這段吧: #gzip_types text/plain application/x-javascript text/css text/javascript application/x-httpd-php image/jpeg image/gif image/png; #gzip公共配置 gzip_http_version 1.1; #識別http的協議版本(1.0/1.1) gzip_proxied any; #設定使用代理時是否進行壓縮,預設是off的 gzip_vary on; #和http頭有關係,加個vary頭,代理判斷是否需要壓縮 gzip_disable "MSIE [1-6]."; #禁用IE6的gzip壓縮
cat proxy.conf
proxy_temp_path /tmp/proxy_temp; proxy_cache_path /tmp/proxy_cache levels=1:2 keys_zone=cache_one:500m inactive=1d max_size=3g; #client_body_buffer_size 512k; #原為512k proxy_connect_timeout 50; #代理連線超時 proxy_read_timeout 600; #代理髮送超時 proxy_send_timeout 600; #代理接收超時 proxy_buffer_size 128k; #代理緩衝大小,原為32k proxy_buffers 16 256k; #代理緩衝,原為4 64k proxy_busy_buffers_size 512k; #高負荷下緩衝大小,原為128k proxy_temp_file_write_size 1024m; #proxy快取臨時檔案的大小原為128k #proxy_ignore_client_abort on; #不允許代理端主動關閉連線 proxy_next_upstream error timeout invalid_header http_500 http_503 http_404 http_502 http_504; ~
cat host.conf
server { listen 80; server_name localhost; #charset GB2312; location /MFront { proxy_next_upstream http_502 http_504 error timeout invalid_header; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $http_x_forwarded_for; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_redirect off; proxy_http_version 1.1; proxy_set_header Connection ""; proxy_pass http://MFront; } location /Front { proxy_next_upstream http_502 http_504 error timeout invalid_header; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $http_x_forwarded_for; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_redirect off; proxy_http_version 1.1; proxy_set_header Connection ""; proxy_pass http://Front; } }
cat web.conf
upstream MFront { server 127.0.0.1:8080 max_fails=1 fail_timeout=60s; server 127.0.0.1:8081 max_fails=1 fail_timeout=60s; } upstream Front { server 127.0.0.1:8080 max_fails=1 fail_timeout=60s; server 127.0.0.1:8081 max_fails=1 fail_timeout=60s; }
特別要注意web.conf配置檔案中的專案要和host.conf配置檔案中的專案名稱保持一致
接下來在url中輸入:http://localhost/MFront/
同樣顯示內容會在MFronttesta和MFronttestb之間切換說明配置正確
配置nginx啟動
vim /etc/init.d/nginx
#!/bin/bash # chkconfig: 345 99 20 # description: nginx PROG="/usr/local/nginx/sbin/nginx" PIDF="/usr/local/nginx/logs/nginx.pid" case "$1" in start) $PROG echo "Nginx servicestart success." ;; stop) kill -s QUIT $(cat $PIDF) echo "Nginx service stopsuccess." ;; restart) $0 stop $0 start ;; reload) kill -s HUP $(cat $PIDF) echo"reload Nginx configsuccess." ;; *) echo "Usage: $0{start|stop|restart|reload}" exit 1 esac
授予可執行檔案 chmod +x /etc/init.d/nginx
#新增到啟動服務 chkconfig --add nginx #配置自動啟動 chkconfig --level 2345 nginx on
總結
對於配置檔案中還有很多引數的調配這裡就暫時不做細說,如果後面有時間的話會單獨講。