Nginx+tomcat server-side environment configuration detailed _nginx

Source: Internet
Author: User
Tags mkdir rrd rrdtool java web tomcat tomcat server

Nginx+tomcat is the current mainstream Java Web architecture, how to let nginx+tomcat work at the same time, can also say how to use Nginx to reverse proxy tomcat back-end equalization? The direct installation configuration is as follows:

1. JAVA JDK Installation:

#下载相应的jdk软件包 and then unzip the installation, I have the package name here: jdk-7u25-linux-x64.tar.gz

Tar-xzf jdk-7u25-linux-x64.tar.gz; mkdir-p/usr/java/; MV jdk1.7.0_25//usr/java/. 


#然后配置环境变量 so that you can refer to the JDK anywhere, and configure the following:

#vi Add the following statement to the/etc/profile last face:

Export java_home=/usr/java/jdk1.7.0_25 
 
export classpath= $CLASSPATH: $JAVA _home/lib: $JAVA _home/jre/lib 
 
Export path= $JAVA _home/bin: $JAVA _home/jre/bin: $PATH: $HOMR/bin 


#source/etc/profile #使环境变量马上生效

#java--version #查看java版本, seeing the jdk1.7.0_25 version is a successful installation of the Java JDK.
2, Nginx installation:

wget http://nginx.org/download/nginx-1.2.6.tar.gz 
 
useradd www 
 
tar zxvf nginx-1.2.6.tar.gz 
 
CD nginx-1.2.6/ 
 
./configure--user=www--group=www--prefix=/usr/local/nginx 
 \--with-http_stub_status_module --with-http_ssl_module make 
 
&& make install 


#Nginx安装完毕, and then use the command:/usr/local/nginx/sbin/nginx-t Test OK, on behalf of Nginx installation success.

/usr/local/nginx/sbin/nginx carriage return starts Nginx and you can see the Nginx default page by accessing http://ip/.
3, Tomcat installation:

#官方网站下载tomcat 6.0.30 or other version:

CD/USR/SRC && tar xzf apache-tomcat-6.0.30.tar.gz 


#直接解压就可以使用, decompression completes execution, simultaneously copies two Tomcat, named Tomcat1 TOMCAT2

MV Apache-tomcat-6.0.30/usr/local/tomcat1 
 
Cp/usr/local/tomcat1/usr/local/tomcat2-r 


#分别修改tomcat1和tomcat2 Port, there are three ports that need to be modified, respectively, as follows:

Shutdown Port: 8005 is mainly responsible for starting off.

AJP Port: 8009 is primarily responsible for balancing through AJP (commonly used in Apache and Tomcat integration)

HTTP port: 8080 can be accessed directly through the Web page (nginx+tomcata consolidation)

#注 * If the TOMCAT1 three ports are: 8005 8009 8080, then TOMCAT2 ports on this basis are +1, that is: 8006 8010 8081

#一台服务器上, the port cannot be duplicated, otherwise it will be an error.

#修改完端口后, and then start two Tomcat, the start command is:

#如何提示没有这个文件或者权限不足, requires Tomcat's Bin directory to give execution permission to the sh file: chmod o+x *.sh

/usr/local/tomcat1/bin/startup.sh 
 
/usr/local/tomcat2/bin/startup.sh 


#启动后, you can see 6 ports using NETSTAT-TNL, that is, a successful start on behalf of TOMCAT1 TOMCAT2. You can access the Tomcat default page using http://ip:8080 http://ip:8081.
#如果需要修改tomcat发布目录为自己制定的目录, you need to make the following adjustments to create two publishing directories:

Mkdir-p/USR/WEBAPPS/{WWW1,WWW2} 

Edit Vi/usr/local/tomcat1/conf/server.xml The last </Host> previous line add the content:

<context path= "" docbase= "/USR/WEBAPPS/WWW1" reloadable= "false"/> 

Edit Vi/usr/local/tomcat2/conf/server.xml The last </Host> previous line add the content:

<context path= "" docbase= "/USR/WEBAPPS/WWW2" reloadable= "false"/> 

TOMCAT1 Publish Directory content:

 
 

Tomcat2 Publish Directory content:

 
 

Then visit http://ip:8080, 8081 to view the test content.

4, Nginx+tomcat Integration:

Integration is mainly to modify the nginx.conf configuration, to a complete nginx.conf online configuration, some of the parameters can be modified according to the actual requirements:

User www www. 
Worker_processes 8; 
Pid/usr/local/nginx/nginx.pid; 
Worker_rlimit_nofile 102400; 
events {use Epoll; 
Worker_connections 102400; 
 } http {include mime.types; 
 Default_type Application/octet-stream; 
 Fastcgi_intercept_errors on; 
 CharSet Utf-8; 
 Server_names_hash_bucket_size 128; 
 Client_header_buffer_size 4k; 
 Large_client_header_buffers 4 32k; 
 Client_max_body_size 300m; 
 Sendfile on; 
  
 Tcp_nopush on; 
  
 Keepalive_timeout 60; 
 Tcp_nodelay on; 
 
 Client_body_buffer_size 512k; 
 Proxy_connect_timeout 5; 
 Proxy_read_timeout 60; 
 Proxy_send_timeout 5; 
 Proxy_buffer_size 16k; 
 Proxy_buffers 4 64k; 
 Proxy_busy_buffers_size 128k; 
  
 Proxy_temp_file_write_size 128k; 
 gzip on; 
 Gzip_min_length 1k; 
 Gzip_buffers 4 16k; 
 Gzip_http_version 1.1; 
 Gzip_comp_level 2; 
 Gzip_types text/plain application/x-javascript text/css application/xml; 
  
Gzip_vary on; # # #2012 -12-19 change nginx logs Log_format main ' $http _x_forwarded_for-$remoTe_user [$time _local] "$request" "$status $body _bytes_sent" $http _referer "" $http _user_agent "$request _time 
     
$remote _addr '; 
 Upstream Web_app {server 127.0.0.1:8080 weight=1 max_fails=2 fail_timeout=30s; 
Server 127.0.0.1:8081 weight=1 max_fails=2 fail_timeout=30s; 
 } ### #chinaapp. sinaapp.com server {Listen 80; 
 server_name chinaapp.sinaapp.com; 
 Index index.jsp index.html index.htm; 
  
 #发布目录/data/www root/data/www; 
 Location/{proxy_next_upstream http_502 http_504 error timeout invalid_header; 
 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_pass Http://web_app; 
 Expires 3d; 
 } 
  
 } 
 
}

#注 * Server segment Proxy_pass defined Web_app need to be consistent with the Web_app defined in upstream, otherwise the server cannot find a balance.

#如上配置, Nginx+tomcat reverse proxy load balanced configuration is complete, if you want to do static and dynamic separation, just add the following configuration in Nginx OK.

#配置Nginx动静分离

Location ~. *\. (HTML|HTM|GIF|JPG|JPEG|BMP|PNG|ICO|TXT|JS|CSS) $ 
 
{ 
 
root/data/www; 
 
#expires定义用户浏览器缓存的时间为3天, if static pages are infrequently updated, you can set them longer so that you can save bandwidth and ease the pressure on the server 
 
expires  3d; 
 
 

5, open the Nginx monitoring
1), Nginx simple State monitoring

Add the following code in nginx.conf to monitor the current state of Nginx, and then access http://serverip/status to access

Location/status {
stub_status on;
Access_log off;


Generally displayed as

Active connections:16 
Server accepts handled requests 191226 191226 305915 reading:0 writing:1 waiting 
: 15

Ctive Connections: Number of active connections to the backend.

The Server accepts handled Requests:nginx handled 24 connections, successfully creating 24 handshakes (proving that there were no failures), processing 129 requests in total.

Reading:nginx the number of header information read to the client.

Writing:nginx the number of header information returned to the client.

Waiting: When Keep-alive is turned on, this value equals active– (reading + writing), meaning that the nginx has been processed and is waiting for the next request to host the connection.
Note that this module is not compiled into Nginx by default, and if you want to use the module, specify when compiling the installation Nginx:

 ./configure–with-http_stub_status_module 

To see if the installed Nginx contains stub_status modules

 #/usr/local/nginx/sbin/nginx-v 
 
TLS SNI Support disabled
Configure arguments:--prefix=/usr/local/nginx--user=www--group=www Status_module--with-file-aio--with-http_ssl_module 


2), nginx of the graphical monitoring-NGINX-RRD stats

NGINX-RRD is an official Nginx recommended Nginx monitoring tool, the use of NGINX-RRD can be very convenient to generate a chart, easy for us to view.
A, the operating environment (CentOS):

Before installation, you need to install the RRDtool drawing tool and the corresponding Perl module, which can be run first:

Yum install RRDtool Libhtml-parser-perl libwww-perl Librrds-perl

Make sure the RRDtool and the corresponding Perl are installed.

B, installation configuration

Download:

wget http://soft.vpser.net/status/nginx-rrd/nginx-rrd-0.1.4.tgz

Extract:

Tar zxvf nginx-rrd-0.1.4.tgz

Into the NGINX-RRD directory,

CD nginx-rrd-0.1.4/

To copy the main program:

CP usr/sbin/*/usr/sbin

Copy configuration file

CP Etc/nginx-rrd.conf/etc

To copy a timed execution file:

CP ETC/CRON.D/NGINX-RRD.CRON/ETC/CRON.D

To create a NGINX-RRD build directory:

Mkdir/home/wwwroot/nginx && MKDIR/HOME/WWWROOT/NGINX/RRD

CP Html/index.php/home/wwwroot/nginx

Edit/home/wwwroot/nginx/index.php Modify Login Password

<?php
Header ("content-type:text/html; Charset=utf-8 ");

$password = "admin"; 

.........

Edit the configuration file nginx-rrd.conf, after the modification is completed as follows:

#####################################################
#
dir where rrd databases are stored
rrd_dir= " Home/wwwroot/nginx-rrd/";
# dir where PNG images are presented
www_dir= "/home/wwwroot/nginx/";
# process Nice level
nice_level= " -19";
# bin dir
bin_dir= "/usr/sbin";
# Servers to test
# server_utl;server_name
servers_url= "http://127.0.0.1/status;127.0.0.1"

Multiple servers, you can servers_url the hollow grid, the former part of the Nginx_status address, followed by the host domain name.

Severs_url format

Note that the nginx.conf virtual host server{} requires that you have joined:

Location/status {
stub_status on;
Access_log off;


The above setup is complete, you can run it yourself:/usr/sbin/nginx-collect, start the collection program. Cron will generate data in 15 minutes.

If you do not perform the build data regularly, you can add the following/etc/crontab:

* * * * * * * * root/usr/sbin/nginx-collect */15 * * * * * * * *
root/usr/sbin/nginx-graph

Then you can access it by entering and then accessing http://serverip/nginx/.

6, open Tomcat monitoring

1), TOMCAT6 configuration

Add the following code before modifying the </tomcat-users> node in the Tomcat/conftomcat-users.xml file.

 <user username= "admin" password= "admin" roles= "manager"/>

Then you can access it by entering and then accessing Http://serverip:8080/manager/status.

2) Configuration of TOMCAT7

Add the following code before modifying the </tomcat-users> node in the Tomcat/conftomcat-users.xml file.

<role rolename= "Manager-gui"/>
<user username= "Tomcat" admin= "admin" roles= "Manager-gui"/>

Then you can access it by entering and then accessing Http://serverip:8080/manager/status.

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.