天天看点

Nginx自学手册(六)Nginx+Tomcat实现动静分离

(一)简述

    Nginx是一种轻量级,高性能,多进程的Web服务器,非常适合作为静态资源的服务器使用,而动态的访问操作可以使用稳定的Apache、Tomcat及IIS等来实现,这里就以Nginx作为代理服务器的同时,也使用其作为静态资源的服务器,而动态的访问服务器就以Tomcat为例说明。

<a href="https://s4.51cto.com/wyfs02/M02/9E/BA/wKioL1mVOu7hSTagAADS-vIP1pE166.png" target="_blank"></a>

(二)环境简介

服务器名称

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测试界面,打开会出现测试页面。

<code>[root@localhost </code><code>local</code><code>]</code><code># /usr/local/apache-tomcat-7.0.63/bin/startup.sh </code>

<code>Using CATALINA_BASE:   </code><code>/usr/local/apache-tomcat-7</code><code>.0.63</code>

<code>Using CATALINA_HOME:   </code><code>/usr/local/apache-tomcat-7</code><code>.0.63</code>

<code>Using CATALINA_TMPDIR: </code><code>/usr/local/apache-tomcat-7</code><code>.0.63</code><code>/temp</code>

<code>Using JRE_HOME:        </code><code>/usr/java/jdk1</code><code>.8.0_131/</code>

<code>Using CLASSPATH:       </code><code>/usr/local/apache-tomcat-7</code><code>.0.63</code><code>/bin/bootstrap</code><code>.jar:</code><code>/usr/local/apache-tomcat-7</code><code>.0.63</code><code>/bin/tomcat-juli</code><code>.jar</code>

<code>Tomcat started.</code>

1.3新建测试页面。在/usr/local/apache-tomcat-7.0.63/webapps下新建html目录和index.html文件

<code>[root@localhost </code><code>local</code><code>]</code><code># mkdir /usr/local/apache-tomcat-7.0.63/webapps/html</code>

<code>[root@localhost </code><code>local</code><code>]</code><code># vim /usr/local/apache-tomcat-7.0.63/webapps/html/index.html</code>

<code>this is tomcat index.html</code>

<code>and this ip:192.168.180.23</code>

<code>port:8080</code>

1.4修改server.xml文件的测试路径的路径。在&lt;host&gt; ****&lt;/host&gt;标签之间添加上:  &lt;Context path="" docBase="html" debug="0" reloadable="true" /&gt;

path是说明虚拟目录的名字,如果你要只输入ip地址就显示主页,则该键值留为空;

docBase是虚拟目录的路径,它默认的是$tomcat/webapps/ROOT目录,现在我在webapps目录下建了一个html目录,让该目录作为我的默认目录。

debug和reloadable一般都分别设置成0和true。

<code>[root@localhost </code><code>local</code><code>]</code><code># vim /usr/local/apache-tomcat-7.0.63/conf/server.xml </code>

<code>   </code><code>&lt;Host name=</code><code>"localhost"</code>  <code>appBase=</code><code>"webapps"</code>                                                                                                                                                 

<code>            </code><code>unpackWARs=</code><code>"true"</code> <code>autoDeploy=</code><code>"true"</code><code>&gt;                                                                                                                                                </code>

<code>                                                                                                                                                                                                </code> 

<code>        </code><code>&lt;!-- SingleSignOn valve, share authentication between web applications                                                                                                                  </code>

<code>             </code><code>Documentation at: </code><code>/docs/config/valve</code><code>.html --&gt;                                                                                                                                      </code>

<code>        </code><code>&lt;!--                                                                                                                                                                                    </code>

<code>        </code><code>&lt;Valve className=</code><code>"org.apache.catalina.authenticator.SingleSignOn"</code> <code>/&gt;                                                                                                                    </code>

<code>        </code><code>--&gt;                                                                                                                                                                                     </code>

<code>       </code><code>&lt;Context path=</code><code>""</code> <code>docBase=</code><code>"html"</code> <code>debug=</code><code>"0"</code> <code>reloadable=</code><code>"true"</code> <code>/&gt;                                                                                                                           </code>

<code>        </code><code>&lt;!-- Access log processes all example.                                                                                                                                                  </code>

<code>             </code><code>Documentation at: </code><code>/docs/config/valve</code><code>.html                                                                                                                                          </code>

<code>             </code><code>Note: The pattern used is equivalent to using pattern=</code><code>"common"</code> <code>--&gt;                                                                                                                 </code>

<code>        </code><code>&lt;Valve className=</code><code>"org.apache.catalina.valves.AccessLogValve"</code> <code>directory=</code><code>"logs"</code>                                                                                                           

<code>               </code><code>prefix=</code><code>"localhost_access_log."</code> <code>suffix=</code><code>".txt"</code>                                                                                                                                     

<code>               </code><code>pattern=</code><code>"%h %l %u %t &amp;quot;%r&amp;quot; %s %b"</code> <code>/&gt;                                                                                                                                    </code>

<code>      </code><code>&lt;</code><code>/Host</code><code>&gt;</code>

1.5修改web.xml文件,查看html文件的内容。在&lt;welcome-file-list&gt;****&lt;/welcome-file&gt;段之间添加上:&lt;welcome-file&gt;html&lt;/welcome-file&gt;。完成之后重启即可。

<code>[root@localhost </code><code>local</code><code>]</code><code># vim /usr/local/apache-tomcat-7.0.63/conf/web.xml </code>

<code> </code><code>&lt;welcome-</code><code>file</code><code>-list&gt;</code>

<code>        </code><code>&lt;welcome-</code><code>file</code><code>&gt;html&lt;</code><code>/welcome-file</code><code>&gt;</code>

<code>        </code><code>&lt;welcome-</code><code>file</code><code>&gt;index.html&lt;</code><code>/welcome-file</code><code>&gt;</code>

<code>        </code><code>&lt;welcome-</code><code>file</code><code>&gt;index.htm&lt;</code><code>/welcome-file</code><code>&gt;</code>

<code>        </code><code>&lt;welcome-</code><code>file</code><code>&gt;index.jsp&lt;</code><code>/welcome-file</code><code>&gt;</code>

<code>    </code><code>&lt;</code><code>/welcome-file-list</code><code>&gt;</code>

1.6新建相关的测试页面,如test.jsp test.do

<code>[root@localhost html]</code><code># pwd</code>

<code>/usr/local/apache-tomcat-7</code><code>.0.63</code><code>/webapps/html</code>

<code>[root@localhost html]</code><code># cat test.jsp </code>

<code>this is tomcat of </code><code>test</code><code>.jsp</code>

<code>[root@localhost html]</code><code># cat test.do </code>

<code>welcome to tomcat ,this is </code><code>test</code><code>-</code><code>do</code><code>.</code><code>do</code>

通过浏览器访问的结果如下:

a.访问默认页面:

<a href="https://s5.51cto.com/wyfs02/M02/9E/BB/wKioL1mVRC2R-j3XAAAvYC9n5xU396.png" target="_blank"></a>

c.访问test.do页面

<a href="https://s2.51cto.com/wyfs02/M02/00/0C/wKiom1mVRGSyzy9CAAAyjhWlLKc973.png" target="_blank"></a>

(2)Nginx服务器的相关配置

<code>[root@Monitor server]</code><code># vim /usr/local/nginx/conf/server/server.conf</code>

<code>server {</code>

<code>         </code><code>listen    80;</code>

<code>         </code><code>server_name  xn2.lqb.com;</code>

<code>         </code><code>root </code><code>/html/xn2</code><code>;</code>

<code>    </code><code>#    rewrite ^/(.*)$ https:xn3.lqb.com/$1 permanent;</code>

<code>     </code><code>location / {</code>

<code>       </code><code>index index.html;</code>

<code>    </code><code>#    proxy_cache mycache;</code>

<code>    </code><code>#    proxy_cache_valid 200 3h;</code>

<code>    </code><code>#    proxy_cache_valid 301 302 10m;</code>

<code>    </code><code>#    proxy_cache_valid all 1m;</code>

<code>    </code><code>#    proxy_cache_use_stale error timeout http_500 http_502 http_503;</code>

<code>    </code><code>#    proxy_pass http://192.168.180.9;</code>

<code>    </code><code>#    proxy_set_header Host    $host;</code>

<code>    </code><code>#    proxy_set_header X-Real-IP  $remote_addr;</code>

<code>                     </code><code>}</code>

<code>       </code><code>location  </code><code>/ie/</code>

<code>           </code><code>{</code>

<code>             </code><code>index index.html;</code>

<code>             </code><code>}</code>

<code>       </code><code>location ~ .*\.(gif|jpg|jpeg|png|swf)$  {      </code><code>#所有的静态文件以gif、jpg等结尾的都在本地打开,目录为/html/xn2下,保存时间为30天</code>

<code>                     </code><code>expires 30d;</code>

<code>                          </code><code>}</code>

<code>       </code><code>location ~ (\.jsp)|(\.</code><code>do</code><code>)$ {                 </code><code>##########所有的以jsp .do的动态请求都交给后边的tomcat代理服务器处理。</code>

<code>                </code><code>proxy_pass http:</code><code>//192</code><code>.168.180.23:8080;</code>

<code>                </code><code>proxy_redirect off;</code>

<code>                </code><code>proxy_set_header HOST $host;</code>

<code>                </code><code>proxy_set_header X-Real-IP $remote_addr;</code>

<code>                </code><code>proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;</code>

<code>                       </code><code>}</code>

<code>                    </code><code>}</code>

<code>[root@Monitor server]</code><code># /usr/local/nginx/sbin/nginx -t</code>

<code>nginx: the configuration </code><code>file</code> <code>/usr/local/nginx/conf/nginx</code><code>.conf syntax is ok</code>

<code>nginx: configuration </code><code>file</code> <code>/usr/local/nginx/conf/nginx</code><code>.conf </code><code>test</code> <code>is successful</code>

<code>[root@Monitor server]</code><code># /usr/local/nginx/sbin/nginx -s reload</code>

访问结果如下:

a.访问默认页面,会直接访问nginx所默认的页面,而不会调到后台tomcat服务器页面

<a href="https://s5.51cto.com/wyfs02/M00/9E/BC/wKioL1mVRu6C15E5AAAzsUM36yY644.png" target="_blank"></a>

b.访问.do页面。其实访问的就是tomcat后台test.do页面

<a href="https://s1.51cto.com/wyfs02/M02/9E/BC/wKioL1mVRsXjZs46AAAyjhWlLKc615.png-wh_500x0-wm_3-wmp_4-s_1838735021.png" target="_blank"></a>

c.访问jsp页面。其实访问的就是tomcat后台test.jsp页面

<a href="https://s2.51cto.com/wyfs02/M02/9E/BC/wKioL1mVRyzRiGlGAAAvYC9n5xU371.png" target="_blank"></a>

至此nginx+tomcat的动静分离配置完成。

本文转自 lqbyz 51CTO博客,原文链接:http://blog.51cto.com/liqingbiao/1957094

继续阅读