2014-04-17 12:29:01
来 源
itjs.cn
Nginx
本文介绍配置Nginx+tomcat服务器环境和负载均衡,希望对于初学Nginx服务器相关的朋友有帮助,更多Nginx安装、配置、报错处理等资源请本站内搜索。。
一、安装软件

1、安装nginx 和JDK

# cd  /usr/ports/www/nginx && make install clean

# cd /usr/ports/java/jdk16 && make install clean

这里会报错

Due to licensing restrictions, certain files must be fetched manually.

 Please download the Update 3 Source from

 http://www.java.net/download/jdk6/6u3/promoted/b05/jdk-6u3-fcs-src-b05-jrl-24_sep_2007.jar

 and the Source Binaries from

 http://www.java.net/download/jdk6/6u3/promoted/b05/jdk-6u3-fcs-bin-b05-jrl-24_sep_2007.jar

 and the Mozilla Headers from

 http://www.java.net/download/jdk6/6u3/promoted/b05/jdk-6u3-fcs-mozilla_headers-b05-unix-24_sep_2007.jar

 .

 Please open http://www.oracle.com/technetwork/java/javase/downloads/index.html

 in a web browser and follow the "Download" link for

 "JDK DST Timezone Update Tool - 1_3_45" to obtain the

 time zone update file, tzupdater-1_3_45-2011n.zip.

 Please download the patchset, bsd-jdk16-patches-4.tar.bz2, from

 http://www.eyesbeyond.com/freebsddom/java/jdk16.html.

 Please place the downloaded file(s) in /usr/ports/distfiles

 and restart the build.

按提示操作

#axel -an 10 http://www.java.net/download/jdk6/6u3/promoted/b05/jdk-6u3-fcs-src-b05-jrl-24_sep_2007.jar

#axel -an 10 http://www.java.net/download/jdk6/6u3/promoted/b05/jdk-6u3-fcs-bin-b05-jrl-24_sep_2007.jar 

#axel -an 10 http://www.java.net/download/jdk6/6u3/promoted/b05/jdk-6u3-fcs-mozilla_headers-b05-unix-24_sep_2007.jar

下面三个文件下载放到上述同样的目录

tzupdater-1_3_34-2010o.zip http://www.oracle.com/technetwork/java/javase/downloads/tzupdater-download-513681.html

bsd-jdk16-patches-4.tar.bz2 ftp://www.kuzbass.ru/pub/freebsd/jdk16/bsd-jdk16-patches-4.tar.bz2

diablo-latte-freebsd7-amd64-1.6.0_07-b02.tar.bz2 http://www.FreeBSDFoundation.org/cgi-bin/download?download=diablo-caffe-freebsd7-amd64-1.6.0_07-b02.tar.bz2

#cd /usr/ports/java/jdk16 && make install clean

2、配置环境

3、安装tomcat

# /usr/ports/www/tomcat6 && make install

#echo 'tomcat60_enable="YES"' >> /etc/rc.conf

#/usr/local/etc/rc.d/tomcat6 start

配置端口

#vim /usr/local/apache-tomcat-6.0/conf/server.xml

omcat的配置可以通过两种方式来配置。

1.通过/conf/server.xml来配置。

2.通过可视化的界面来配置。

下面来介绍server.xml的配置:

按装完Tomcat后它的server.xml的配置内容是这样的:

connectionTimeout="2000"

它表式的是:一个客房端连接到服务器如果没有任何动作它如果超过这个时间那么它就视为无效。

2000表式的是毫秒。

maxThreads="150"

它表示的是同一时间连接到服务器上的同一个页面时的最大连接数。

里面有三个端口配置

<Connector port="8180" protocol="HTTP/1.1"

connectionTimeout="20000"

redirectPort="8443" />

在 <Host> 和 </Host> 之间加入

<Context path="/myapp" docBase="/data/vhosts/www.example.com" debug="0" reloadable="true" crossContext="true" />

二、配置

1、nginx.conf添加tomcat反向代理

#vim /usr/local/etc/nginx/nginx.conf

upstream tomcat_server{ 

server 127.0.0.1:8180;

}

2、站点配置文件使用反向代理。

#vim /usr/local/etc/nginx/vhosts/www.example.com.conf

添加

location ~ .*.(gif|jpg|jpeg|png|bmp|swf)$ {

root  /data/vhosts/www.example.com;

}

location ~ .*.(jsp|do)$ {

proxy_pass http://tomcat_server;

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;

}

#nginx -t

三、测试

#/usr/local/etc/rc.d/nginx reload

#ee /data/vhosts/www.example.com/test.jsp

<HTML>

<HEAD>

<TITLE>JSP测试页面</TITLE>

</HEAD>

<BODY>

<%out.println("<h1>Hello World! </h1>");%>

</BODY>

</HTML>

#cp ~diege/test.jpg /data/vhosts/www.example.com/

访问测试

0101 Firefox/11.0"

172.17.2.160 - - [25/Apr/2012:14:59:10 +0800] "GET /test.jsp HTTP/1.1" 404 979 "-" "Mozilla/5.0 (Windows NT 5.1; rv:11.0) Gecko/20100101 Firefox/11.0"

cp /data/vhosts/www.example.com/test.jsp /usr/local/apache-tomcat-6.0/work/Catalina/www.example.com/

不行

cp /data/vhosts/www.example.com/test.jsp  /usr/local/apache-tomcat-6.0/webapps/ROOT/

172.17.2.160 - - [25/Apr/2012:15:14:10 +0800] "GET /test.jsp HTTP/1.1" 200 101 "-" "Mozilla/5.0 (Windows NT 5.1; rv:11.0) Gecko/20100101 Firefox/11.0"

新定义一个端口和站点

四、多个tomcat构建负载均衡

#vim /usr/local/etc/nginx/nginx.conf

upstream tomcat_server{ 

server 192.168.1.100:8080;

server 192.168.1.110:8080;

server 192.168.1.120:8080;

}

声明: 此文观点不代表本站立场;转载须要保留原文链接;版权疑问请联系我们。