由於目錄已將項目正式部署並發布了,但由於時不時地會出現bug,修複bug再次提交後,會讓項目出現短時間的無法訪問的問題,雖然時間雖短,但還是會影響使用者的體驗。為了不讓使用者察覺出項目的變動,於是我便採用了用nginx來實現負載平衡,主要步驟記錄如下:
1.配置nginx安裝源
vim /etc/yum.repos.d/nginx.repo
#nginx.repo
[nginx] name=nginx repo baseurl=http://nginx.org/packages/centos/7/$basearch/ gpgcheck=0 enabled=1
2.安裝nginx
如已安裝了nginx,需要升級,執行update即可
yum update nginx
如未安裝,執行如下命令,安裝即可
yum install nginx -y
nginx -v //查看版本
3.查看nginx的安裝目錄
whereis nginx
4.編輯nginx設定檔
cd /etc/nginx
vim nginx.conf
在此,附上nginx.conf的源碼:
user nginx;worker_processes 1;error_log /var/log/nginx/error.log warn;pid /var/run/nginx.pid;events { 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; #tcp_nopush on; keepalive_timeout 65; #gzip on; include /etc/nginx/conf.d/*.conf;}從上面可以看出,還調用了/etc/nginx.conf.d/*.conf檔案
再附上default.conf源碼:
upstream school {server 192.168.1.103:80;server 192.168.1.100:8080;server 192.168.1.101:80;}server { listen 80; server_name school; #charset koi8-r; #access_log /var/log/nginx/log/host.access.log main; location / { # root /usr/share/nginx/html;proxy_pass http://school; # index index.html index.htm; } #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; }}如此一來,就完成了3台伺服器的簡單負載了。
5.測試nginx配置是否正確
nginx -t
都顯示的ok和successful表示配置沒得什麼問題
6.啟動nginx
啟動下nginx就可以了
service nginx start
再輸入nginx的地址測試一下:
由此看出,每執行一次,所到達的tomcat服務都是不一樣的,OK!大功告成!!!
以上就介紹了採用nginx讓多個tomcat實現負載平衡,包括了方面的內容,希望對PHP教程有興趣的朋友有所協助。