(一)简述html
Nginx是一种轻量级,高性能,多进程的Web服务器,很是适合做为静态资源的服务器使用,而动态的访问操做能够使用稳定的Apache、Tomcat及IIS等来实现,这里就以Nginx做为代理服务器的同时,也使用其做为静态资源的服务器,而动态的访问服务器就以Tomcat为例说明。java
(二)环境简介nginx
服务器名称 | IP | 备注 |
Nginx服务器 | 192.168.180.4 | |
Tomcat服务器 | 192.168.180.23 | |
client | 192.168.181.231 | 客户端访问 |
(三)具体步骤:web
(1)tomcat服务器(192.168.180.23)的相关配置配置apache
1.1 tomcat的安装及相关环境变量的配置能够参考前面文档,具体本次试验省略了bootstrap
1.2 ,启动tomcat测试界面,打开会出现测试页面。vim
[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文件浏览器
[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" />tomcat
path是说明虚拟目录的名字,若是你要只输入ip地址就显示主页,则该键值留为空;
docBase是虚拟目录的路径,它默认的是$tomcat/webapps/ROOT目录,如今我在webapps目录下建了一个html目录,让该目录做为个人默认目录。
debug和reloadable通常都分别设置成0和true。bash
[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>。完成以后重启便可。
[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
[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服务器的相关配置
[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的动静分离配置完成。