今天在centos上安裝了兩個tomcat和nginx,進行配置。今天記錄的只是最基本的實現測試。(不包含使用redis進行session共享)
Nginx 是一款輕量級的Web 服務器/反向代理服務器及電子郵件(IMAP/POP3)代理服務器,並在一個BSD-like 協議下發行。 其特點是佔有內存少,併發能力強。
直接開始主題:
1,首先jdk應該是配好了我就不寫了,安裝nginx(我使用的rmp安裝) ,安裝 pcre 讓nginx支持rewrite,我使用的是pcre2-10.00.tar.gz;
PCRE下載地址: ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/
tar zxvf pcre- 8.01.tar.gz
cd pcre- 8.01
./configure
make
make install
ps:(網上說沒有安裝openssl還要安裝openssl。)
2,安裝nginx:
rpm -ivh nginx-release-centos-6-0.el6.ngx.noarch.rpm
yum install nginx (我是一路的[y/n] 選擇y,最後出現complete!)
nginx的幾個默認目錄:
whereis nginxnginx: /usr/sbin/nginx /etc/nginx /usr/share/nginx
其中
配置所在目錄:/etc/nginx/
錯誤日誌:/var/log/nginx/error.log
默認站點目錄:/usr/share/nginx/html
3.可能CentOS的防火牆把80端口攔住了,打開80端口
iptables -I INPUT -p tcp --dport 80 -j ACCEPT
/etc/init.d/iptables status 查看
出現這個就哦了。
4,配置nginx:
配置文件在/etc/nginx/
#Nginx所用用戶和組 user root; worker_processes 4; error_log /var/log/nginx/error.log warn; pid /var/run/nginx.pid; events { #使用網絡IO模型linux建議epoll,FreeBSD建議採用kqueue use epoll; #允許最大連接數 worker_connections 2048; } 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; upstream localhost { #ip_hash server localhost:8081; server localhost:8080; } #這裏還包含另外的cong文件 include /etc/nginx/conf.d/*.conf; }
主要是upstream 所以一會將要把兩個tomcat端口設置成8080和8081.
另一個配置文件就是那個包含的 /etc/nginx/conf.d/*.conf; (也就是default.conf):
#charset koi8-r; #access_log /var/log/nginx/log/host.access.log main; #修改就是這一段代理 location / { proxy_connect_timeout 3; proxy_send_timeout 30; proxy_read_timeout 30; proxy_pass http://localhost; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html #
5.啓動nginx(我是先配置好兩個tomcat,然後再測試的,繼續往下)
nginx
測試nginx配置:nginx -t
6,開始配置tomcat
tomcat都很熟悉,這次只需要修改server.xml配置文件即可。修改3處:
a:
<!-- 修改port端口:18080 兩個tomcat不能重複--><Server port="18080" shutdown="SHUTDOWN">
b:
<!-- port="8080" tomcat監聽端口 -->
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
c: Engine元素增加jvmRoute屬性:(我是 tomcat1 和 tomcat2 )
<Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat1">
另一個也是改這三處,但是端口要和nginx一致。我的是8080和8081
7.測試:啓動兩個tomcat和nginx。
驗證配置負載均衡設置, http://localhost/ 多次訪問測試:
我爲了方便看。這兩個tomcat的首頁我添加了一個小標識。說明訪問了不同的tomcat。
8.後續還需要加上redis緩存實現session共享,根據自己情況進行測試。
本文來自:Linux學習網