Nginx + Tomcat dynamic/static separation and Nginx optimization (Enterprise case)

Source: Internet
Author: User
Tags format definition nginx load balancing

Objective: nginx processes static pages of user requests. tomcat processes jsp pages of user requests to achieve dynamic separation. nginx processes static pages more efficiently than tomcat, which improves concurrency, processing performance.
Prepare software:
Download jdk1.7: http://www.Oracle.com/technetwork/java/javase/downloads/jdk7-downloads-1880260.html
Download tomcat8.0: http://tomcat.apache.org/download-80.cgi
Download nginx1.4.4: http://nginx.org/en/download.html
1. JDK Configuration
[Root @ localhost ~] # Tar xvfjdk-7u45-linux-x64.tar.gz
[Root @ localhost ~] # Mv jdk1.7.0 _ 45 // usr/local/jdk
[Root @ localhost ~] # Vi/etc/profile
JAVA_HOME =/usr/local/jdk
PATH = $ PATH: $ JAVA_HOME/bin
CLASSPATH =.: $ JAVA_HOME/lib: $ JAVA_HOME/jre/lib
Export JAVA_HOME PATHCLASSPATH
[Root @ localhost ~] # Source/etc/profile
[Root @ localhost ~] # Java-version # The version description is displayed successfully.
Java version "1.7.0 _ 45"

2. Tomcat configuration

[Root @ localhost ~] # Tar zxvfapache-tomcat-8.0.0-RC5.tar.gz
[Root @ localhost ~] # Music apache-tomcat-8.0.0-RC5/usr/local/tomcat

# Tomcat runs as root by default, which is not secure. We set it to use a common user.

[Root @ localhost ~] # Groupadd tomcat
[Root @ localhost ~] # Useradd-g tomcat
[Root @ localhost ~] # Passwd tomcat
[Root @ localhost ~] # Chown tomcat. tomcat-R/usr/local/tomcat
[Root @ localhost ~] # Su-tomcat/usr/local/tomcat/bin/startup. sh
[Root @ localhost ~] # Echo "su-tomcat/usr/local/tomcat/bin/startup. sh">/etc/rc. local # Start upon startup

3. install and configure Nginx

[Root @ localhost ~] # Groupaddnginx
[Root @ localhost ~] # Useradd-gnginx-s/sbin/nologin nginx
[Root @ localhost ~] # Yuminstall-y make zlib-devel openssl-devel pcre-devel
[Root @ localhost ~] # Tar zxvfnginx-1.4.4.tar.gz
[Root @ localhost ~] # Cdnginx-1.4.4
[Root @ localhost nginx-1.4.4] #./configure -- prefix =/usr/local/nginx -- with-http_ssl_module -- with-http_gzip_static_module -- with-http_stub_status_module
[Root @ localhost nginx-1.4.4] # make & make install

# Master configuration file configuration

[Root @ localhost ~] # Vi/usr/local/nginx/conf/nginx. conf
User nginx;
Worker_processes 1;
Error_log logs/error. log;
Pid logs/nginx. pid;
Events {
Worker_connections 1024;
}
Http {
Include mime. types;
Default_type application/octet-stream;
# Log format definition
Log_format main '$ remote_addr-$ remote_user [$ time_local] "$ request "'
'$ Status $ body_bytes_sent "$ http_referer "'
'"$ Http_user_agent" "$ http_x_forwarded_for "';
Access_log logs/access. log main;
Sendfile on;
Keepalive_timeout 65;
# Gzip compression settings
Gzip on;
Gzip_min_length 1 k;
Gzip_buffers 4 16 k;
Gzip_http_version 1.0;
Gzip_comp_level 2;
Gzip_types text/plain application/x-javascripttext/css application/xml;
Gzip_vary on;
Server {
Listen 80;
Server_name www.test.com;
Location /{
# Jsp website program root directory. Generally, nginx and tomcat are in the same directory.
Root/usr/local/tomcat/webapps/ROOT;
Index index.html index. jsp index.html;
}
Location ~ . *. Jsp $ {
Index. jsp;
Proxy_pass http: // 127.0.0.1: 8080; # submit the jsp request to tomcat for processing
Proxy_set_header Host $ host;
Proxy_set_header X-Real-IP $ remote_addr;
Proxy_set_header X-Forwarded-For $ proxy_add_x_forwarded_for;
Proxy_redirect off;
Proxy_set_header Host $ host; # the backend Web server can use X-Forwarded-For to obtain the user's real IP address.
Proxy_set_header X-Real-IP $ remote_addr;
Proxy_set_header X-Forwarded-For $ proxy_add_x_forwarded_for;
Client_max_body_size 10 m; # maximum number of bytes allowed for client requests per file
Client_body_buffer_size 128 k; # maximum number of bytes for the buffer proxy to buffer client requests,
Proxy_connect_timeout 90; # timeout for nginx connection to backend servers (proxy connection timeout)
Proxy_read_timeout 90; # response time of the backend server after successful connection (proxy receiving timeout)
Proxy_buffer_size 4 k; # Set the buffer size for the proxy server (nginx) to save user header information
Proxy_buffers 6 32 k; # proxy_buffers buffer, if the average web page is below 32 k, this setting
Proxy_busy_buffers_size 64 k; # buffer size under high load (proxy_buffers * 2)
Proxy_temp_file_write_size 64 k; # sets the cache folder size. If it is greater than this value, it will be transmitted from the upstream Server
}
Location ~ . * \. (Gif | jpg | png | bmp | swf) $ # process static pages by nginx
{
Expires 30d; # Use the expires cache module and cache it to the client for 30 days
}
Location ~ . * \. (Jsp | js | css )? $
{
Expires 1d;
}
Error_page 404/404 .html; # error page
Error_page 500 502 503 x.html;
Location =/50x.html {
Root html;
}
}
}

# Compile SysV management scripts for nginx startup, stop, and restart for ease of use

[Root @ localhost ~] # Vi/etc/init. d/nginx
#! /Bin/bash
# Chkconfig: 345 99 20
# Description: Nginx servicecontrol script
PROG = "/usr/local/nginx/sbin/nginx"
PIDF = "/usr/local/nginx/logs/nginx. pid"
Case "$1" in
Start)
$ PROG
Echo "Nginx servicestart success ."
;;
Stop)
Kill-s QUIT $ (cat $ PIDF)
Echo "Nginx service stopsuccess ."
;;
Restart)
$0 stop
$0 start
;;
Reload)
Kill-s HUP $ (cat $ PIDF)
Echo "reload Nginx configsuccess ."
;;
*)
Echo "Usage: $0 {start | stop | restart | reload }"
Exit 1
Esac


[Root @ localhost ~] # Chmod + x/etc/init. d/nginx
[Root @ localhost ~] # Service nginx restart
[Root @ localhost ~] # Chkconfig -- add nginx
[Root @ localhost ~] # Chkconfig nginx on

Nginx details: click here
Nginx: click here

Recommended reading:

 

Configure and optimize reverse proxy and load balancing in Nginx

 

Nginx load balancing: nginx: [emerg] cocould not build the types_hash

 

Nginx Load Balancing module ngx_http_upstream_module details

 

Nginx + Firebug allows the browser to tell you which Server Load balancer distributes requests

 

Ubuntu install Nginx php5-fpm MySQL (LNMP environment setup)

Dynamic/static separation of Nginx and Tomcat

  • 1
  • 2
  • Next Page

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.