Static and dynamic separation, improve Web page access speed

Source: Internet
Author: User
Tags nginx server tomcat server

?

Origins

Some people always do not take me to drink coffee, think drink coffee elegant, eat garlic vulgar, fortunately, no vulgar there is no elegance ... I am vulgar, how to, hit me .... Ha ha

What is dynamic and what is static?

Visit the Web page, we always find that the browser will load a variety of files, HTML files, CSS stylesheets, there are JS scripts, as well as pictures, as well as streaming media and other files, these are actually static files, placed on the server, no need to dynamically generate files that is static files . So what is a dynamic file? For example, Java-written JSP files, need to be compiled into a bytecode file by the compiler, and then run on the Java Virtual machine, run, return to the client a response, and sometimes need to fetch data into the database, then this need to be compiled files called dynamic files .

Dynamic file processing is slower than static file processing speed n times, slow where? First, the dynamic files need to compile, time-consuming, dynamic files to connect to the database, time-consuming, dynamic files need to be organized into an HTTP response, time-consuming, according to the Java process, then, according to the URL, find the JSP file, convert the JSP file into a servlet file, and then form a class file, Then run on the JVM, the JVM may also load additional class files, and then form a response back to the servlet and back to the client.

In the use of static and dynamic separation, the commonly used model is as follows (public number: Ops Linux and Python):

In the use of static and dynamic separation, you need to separate some of the files and the active files, in order to use Nginx to handle static requests, and use Tomcat to handle dynamic requests, which will greatly improve processing speed, why?

Nginx and httpd are designed to deal with static files, very efficient, and directly response by Nginx, reduce the process of back-end forwarding, the dynamic content and static content separation, can improve resource utilization, nginx performance is played, also make Tomcat not so busy, Dynamic server machine consumes performance, such as recycling of various method area objects in Java, heap memory recycling, and so on.

constructing the environment of static and dynamic separation

To build a static and dynamic separation environment, you need to install Nginx, install the JDK, and then install Tomcat, in the following steps:

Compile and install Nginx:

[Email protected] nginx-1.10.1]#/configure--prefix=/usr/local/nginx--with-http_ssl_module--with-http_stub_ Status_module--with-pcre

[[email protected] nginx-1.10.1]# make && make install

[[email protected] ~]#/usr/local/nginx/sbin/nginx (start Nginx)

[[email protected] ~]# netstat-tnlp|grep (view listening port and use browser to access test)

TCP 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 13933/nginx

To install the JDK:

[Email protected] server]# RPM-IVH jdk-8u144-linux-x64.rpm

Preparing ... ########################################### [100%]

1:jdk1.8.0_144 ########################################### [100%]

Unpacking JAR files ...

Tools.jar ...

Plugin.jar ...

Javaws.jar ...

Deploy.jar ...

Rt.jar ...

Jsse.jar ...

Charsets.jar ...

Localedata.jar ...

To configure a Java environment variable:

[Email protected] server]# echo "Export Java_home=/usr/java/latest" >/etc/profile.d/java.sh

[Email protected] server]# echo "Export path= $JAVA _home/bin: $PATH" >>/etc/profile.d/java.sh

[Email protected] server]# cat/etc/profile.d/java.sh

Export Java_home=/usr/java/latest

Export Path=/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin

Test the success of the JDK and environment variables:

To install Tomcat:

[Email protected] server]# TAR-XF apache-tomcat-8.5.20.tar.gz-c/usr/local/

L[[email protected] server]# ln-sv/usr/local/apache-tomcat-8.5.20//usr/local/tomcat

'/usr/local/tomcat ', '/usr/local/apache-tomcat-8.5.20/'

To configure the Tomcat environment variable:

[Email protected] local]# vim/etc/profile.d/tomcat.sh

[email protected] local]# cat!$

cat/etc/profile.d/tomcat.sh

Export Catalina_home=/usr/local/tomcat

Export path= $CATALINA _home/bin: $PATH

To test whether Tomcat was installed successfully:

[[email protected] ~]# Netstat-tnlp|grep Java (Note that when the browser is accessed, add a port number to access it)

TCP 0 0::: 8080:::* LISTEN 1125/java

TCP 0 0:: ffff:127.0.0.1:8005:::* LISTEN 1125/java

TCP 0 0::: 8009:::* LISTEN 1125/java

Create a dynamic file

According to the Java directory hierarchy, write a basic page, as follows:

[Email protected] webapps]# mkdir Kel

[Email protected] webapps]# CD Kel

[Email protected] kel]# ls-l

Total 0

[[email protected] kel]# mkdir {meta-inf,web-inf,classes,lib}

[Email protected] kel]# vim index.jsp

[email protected] kel]# cat index.jsp

<%@ page language= "java"%>

<%@ page import= "java.util.*"%>

<title>java page</title>

<body>

<% out.println ("Hello,world"); %>

</body>

[Email protected] kel]# ls-l

Total 20

Drwxr-xr-x 2 root root 4096 Sep 14:46 classes

-rw-r--r--1 root root 201 Sep 14:46 index.jsp

Drwxr-xr-x 2 root root 4096 Sep 14:46 Lib

Drwxr-xr-x 2 root root 4096 Sep 14:46 Meta-inf

Drwxr-xr-x 2 root root 4096 Sep 14:46 Web-inf

Modify the Tomcat configuration file Server.xml (add a virtual host named www.kel.com, the default path is WebApps below the Kel directory):

<context path= "" docbase= "Kel" reloadable= "true"/>

</Host>

Test access:

[[email protected] conf]# grep "www.kel.com"/etc/hosts (set hostname resolution)

192.168.1.238 www.kel.com

[[email protected] conf]# Curl http://www.kel.com:8080 (accessed using Curl)

<title>java page</title>

<body>

Hello,world

</body>

Configure Nginx

To configure host name resolution:

[[email protected] nginx]# grep "www.kel.com"/etc/hosts

192.168.1.237 www.kel.com

[Email protected] conf]# ls-l nginx.conf (Modify nginx configuration file, the dynamic request is the file suffix JSP or do request forwarding to Tomcat, because it is a virtual host, so the URL must be written on the host name, static page processing on Nginx, static page in the HTML path)

-rw-r--r--1 root root 2727 Sep 15:09 nginx.conf

server_name www.kel.com;

#charset Koi8-r;

#access_log Logs/host.access.log Main;

Location/{

root HTML;

Index index.html index.htm;

}

Location ~* \. (Jsp|do) $ {

Proxy_pass http://www.kel.com:8080;

}

Upload static files to the Nginx HTML directory to test for static file access:

[Email protected] nginx]# ls-l html/1.jpg

-rw-r--r--1 root root 25854 Sep 15:13 html/1.jpg

To access the Nginx server test static file access:

Visit Nginx Server and Test dynamic page access:

Summary

Generally speaking, static and dynamic separation is relatively simple, mainly to let the front-end proxy server directly respond to static requests, so that the front-end proxy server forward the dynamic request to the back-end Tomcat server.

Note that the above is the experimental environment, for Tomcat running users are direct root users, in production, must be modified to other users can not log on, such as Tomcat users.

Public number: Ops Linux and Python

Static and dynamic separation, improve Web page access speed

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.