(一)简述
Nginx是一种轻量级,高性能,多进程的Web服务器,非常适合作为静态资源的服务器使用,而动态的访问操作可以使用稳定的Apache、Tomcat及IIS等来实现,这里就以Nginx作为代理服务器的同时,也使用其作为静态资源的服务器,而动态的访问服务器就以Tomcat为例说明。
(二)环境简介
服务器名称 | IP | 备注 |
Nginx服务器 | 192.168.180.4 | |
Tomcat服务器 | 192.168.180.23 | |
client | 192.168.181.231 | 客户端访问 |
(三)具体步骤:
(1)tomcat服务器(192.168.180.23)的相关配置配置
1.1 tomcat的安装及相关环境变量的配置可以参考前面文档,具体本次试验省略了
1.2 ,启动tomcat测试界面,打开会出现测试页面。
1
2
3
4
5
6
7
|
[root@localhost local ] # /usr/local/apache-tomcat-7.0.63/bin/startup.sh
Using CATALINA_BASE: /usr/local/apache-tomcat-7 .0.63
Using CATALINA_HOME: /usr/local/apache-tomcat-7 .0.63
Using CATALINA_TMPDIR: /usr/local/apache-tomcat-7 .0.63 /temp
Using JRE_HOME: /usr/java/jdk1 .8.0_131/
Using CLASSPATH: /usr/local/apache-tomcat-7 .0.63 /bin/bootstrap .jar: /usr/local/apache-tomcat-7 .0.63 /bin/tomcat-juli .jar
Tomcat started. |
1.3新建测试页面。在/usr/local/apache-tomcat-7.0.63/webapps下新建html目录和index.html文件
1
2
3
4
5
|
[root@localhost local ] # mkdir /usr/local/apache-tomcat-7.0.63/webapps/html
[root@localhost local ] # vim /usr/local/apache-tomcat-7.0.63/webapps/html/index.html
this is tomcat index.html and this ip:192.168.180.23 port:8080 |
1.4修改server.xml文件的测试路径的路径。在<host> ****</host>标签之间添加上: <Context path="" docBase="html" debug="0" reloadable="true" />
path是说明虚拟目录的名字,如果你要只输入ip地址就显示主页,则该键值留为空;
docBase是虚拟目录的路径,它默认的是$tomcat/webapps/ROOT目录,现在我在webapps目录下建了一个html目录,让该目录作为我的默认目录。
debug和reloadable一般都分别设置成0和true。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
[root@localhost local ] # vim /usr/local/apache-tomcat-7.0.63/conf/server.xml
<Host name= "localhost" appBase= "webapps" unpackWARs= "true" autoDeploy= "true" >
<!-- SingleSignOn valve, share authentication between web applications
Documentation at: /docs/config/valve .html -->
<!--
<Valve className= "org.apache.catalina.authenticator.SingleSignOn" />
-->
<Context path= "" docBase= "html" debug= "0" reloadable= "true" />
<!-- Access log processes all example.
Documentation at: /docs/config/valve .html
Note: The pattern used is equivalent to using pattern= "common" -->
<Valve className= "org.apache.catalina.valves.AccessLogValve" directory= "logs" prefix= "localhost_access_log." suffix= ".txt" pattern= "%h %l %u %t "%r" %s %b" />
< /Host >
|
1.5修改web.xml文件,查看html文件的内容。在<welcome-file-list>****</welcome-file>段之间添加上:<welcome-file>html</welcome-file>。完成之后重启即可。
1
2
3
4
5
6
7
|
[root@localhost local ] # vim /usr/local/apache-tomcat-7.0.63/conf/web.xml
<welcome- file -list>
<welcome- file >html< /welcome-file >
<welcome- file >index.html< /welcome-file >
<welcome- file >index.htm< /welcome-file >
<welcome- file >index.jsp< /welcome-file >
< /welcome-file-list >
|
1.6新建相关的测试页面,如test.jsp test.do
1
2
3
4
5
6
|
[root@localhost html] # pwd
/usr/local/apache-tomcat-7 .0.63 /webapps/html
[root@localhost html] # cat test.jsp
this is tomcat of test .jsp
[root@localhost html] # cat test.do
welcome to tomcat ,this is test - do . do
|
通过浏览器访问的结果如下:
a.访问默认页面:
c.访问test.do页面
(2)Nginx服务器的相关配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
[root@Monitor server] # vim /usr/local/nginx/conf/server/server.conf
server { listen 80;
server_name xn2.lqb.com;
root /html/xn2 ;
# rewrite ^/(.*)$ https:xn3.lqb.com/$1 permanent;
location / {
index index.html;
# proxy_cache mycache;
# proxy_cache_valid 200 3h;
# proxy_cache_valid 301 302 10m;
# proxy_cache_valid all 1m;
# proxy_cache_use_stale error timeout http_500 http_502 http_503;
# proxy_pass http://192.168.180.9;
# proxy_set_header Host $host;
# proxy_set_header X-Real-IP $remote_addr;
}
location /ie/
{
index index.html;
}
location ~ .*\.(gif|jpg|jpeg|png|swf)$ { #所有的静态文件以gif、jpg等结尾的都在本地打开,目录为/html/xn2下,保存时间为30天
expires 30d;
}
location ~ (\.jsp)|(\. do )$ { ##########所有的以jsp .do的动态请求都交给后边的tomcat代理服务器处理。
proxy_pass http: //192 .168.180.23:8080;
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;
}
}
[root@Monitor server] # /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx .conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx .conf test is successful
[root@Monitor server] # /usr/local/nginx/sbin/nginx -s reload
|
访问结果如下:
a.访问默认页面,会直接访问nginx所默认的页面,而不会调到后台tomcat服务器页面
b.访问.do页面。其实访问的就是tomcat后台test.do页面
c.访问jsp页面。其实访问的就是tomcat后台test.jsp页面
至此nginx+tomcat的动静分离配置完成。
本文转自 lqbyz 51CTO博客,原文链接:http://blog.51cto.com/liqingbiao/1957094