http://blog.csdn.net/zhoufoxcn/article/details/72872524
http://blog.csdn.net/duguxingfeng/article/details/78929472
安装 tomcat 为测试Nginx反向代理
1.下载
# wget http://mirrors.shuosc.org/apache/tomcat/tomcat-8/v8.0.48/bin/apache-tomcat-8.0.48.tar.gz
2.解压
# tar -zxvf apache-tomcat-8.0.48.tar.gz
3.启动测试
# cd /apache-tomcat-8.0.48/bin
# ./startup.sh
- [root@localhost bin]# ./startup.sh
- Using CATALINA_BASE: /usr/software/apache-tomcat-8.0.48
- Using CATALINA_HOME: /usr/software/apache-tomcat-8.0.48
- Using CATALINA_TMPDIR: /usr/software/apache-tomcat-8.0.48/temp
- Using JRE_HOME: /usr/java/jdk1.8.0_121/jre
- Using CLASSPATH: /usr/software/apache-tomcat-8.0.48/bin/bootstrap.jar:/usr/software/apache-tomcat-8.0.48/bin/tomcat-juli.jar
- Tomcat started.
启动成功 访问:http://localhost:8080
4.打开 nginx的配置文件
# cd /usr/local/nginx/conf
复制一份配置文件 命名为: nginx.conf.bak
#cp nginx.conf nginx.conf.bak
# vim nginx.conf
找到: location / 处,添加: proxy_pass http://localhost:8080; http://localhost:8080;为tomcat的访问路径
- server {
- <strong> listen 80; #nginx的监听端口strong>
- server_name localhost;
-
- #charset koi8-r;
-
- #access_log logs/host.access.log main;
-
- location / {
- root html;
- index index.html index.htm;
- <span style="color:#cc0000;"> proxy_pass http://localhost:8080;span>
- }
-
- #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;
- }
修改之后,保存。重启nginx
# ./usr/local/nginx/sbin/nginx -s reload
访问:http://localhost:80 即可访问 tomcat的路径
四、Nginx 负载均衡
1.此处需要部署两个tomcat 进入到第四步下载的tomcat的路径下
# cp -r apache-tomcat-8.0.48 tomcat1
# cp -rapache-tomcat-8.0.48 tomcat2
2.修改tomcat配置文件
tomcat1 端口设定为8080 ;tomcat1的配置无需修改
tomcat2 端口设定为8090
# cd /tomcat2/conf
# vim server.xml
修改
8005" shutdown="SHUTDOWN">
为:8015" shutdown="SHUTDOWN">
8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" />
为:8090" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" />
8009" protocol="AJP/1.3" redirectPort="8443" />
为: 8019" protocol="AJP/1.3" redirectPort="8443" />
3.在tomcat1和tomcat2下创建测试站点
# cd /tomcat1/webapp
# madir test
# vi index.html
添加
this is tomcat1 index
esc :wq 保存推出。
同理在tomcat2下创建test与indexhtml,添加this is tomcat2 index
# cd /tomcat2/webapp
# madir test
# vi index.html
添加
this is tomcat2 index
esc :wq 保存推出。
同时为便于识别,修改监听8081端口的Tomcat的root目录下的index.jsp页面,将首页中的“
If you're seeing this, you've successfully installed Tomcat. Congratulations!”
改为“If you're seeing this, you've successfully installed Tomcat. Congratulations!Server port:8081!”。
4.修改Nginx的 nginx.conf配置文件
# vim /usr/local/nginx/conf/nginx.conf
- http {
- include 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 logs/access.log main;
-
- sendfile on;
- #tcp_nopush on;
-
- #keepalive_timeout 0;
- keepalive_timeout 65;
-
- #gzip on;
-
- <span style="color:#ff0000;">upstream mytomcat {
- server localhost:8080 weight=100;
- server localhost:8090 weight=110;
- }span>
- server {
- listen 80;
- server_name <span style="color:#ff0000;">mytomcatspan>;
-
- #charset koi8-r;
-
- #access_log logs/host.access.log main;
-
- location / {
- root html;
- index index.html index.htm;
- <span style="color:#ff0000;"> proxy_pass http://mytomcat;span>
- }
-
- #error_page 404 /404.html;
-
- # redirect server error pages to the static page /50x.html
- #
- error_page 500 502 503 504 /50x.html;
保存后退出
分别启动tomcat1与tomcat2
# /tomcat1/bin/startup.sh
# /tomcat2/bin/startup.sh
分别访问 http://localhost:8080/test1与http://localhost:8090/test2 验证是否正常
重启 nginx
# /usr/local/nginx/sbin/nginx -s reload
打开浏览器输入:http://localhost/test 多刷新几次,就会看到请求回来的内容有时不一样。说明,nginx 是随机访问的tomcat1或者tomcat2
配置文件里面的weight哪个数值越大,请求到的次数就会多。