Nginx + Tomcat is currently the mainstream Java Web architecture. How can we make nginx + Tomcat work at the same time? Can we also say how to use nginx to reverse proxy Tomcat backend balancing? Let's take a look at it in detail.
Nginx + Tomcat is currently the mainstream Java Web architecture. How can we make nginx + Tomcat work at the same time? Can we also say how to use nginx to reverse proxy Tomcat backend balancing? Directly install the configuration as follows:
1. install Java JDK:
- # Download the appropriate JDK software package, and then unzip the installation, here I package name: 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.
-
- # Configure the environment variable so that JDK can be referenced anywhere. The configuration is as follows:
-
- # Add the following statement to the end of VI/etc/profile:
-
- 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 # make the environment variable take effect immediately
-
- # JAVA -- version # Check the Java version. If JDK 1.7.0 _ 25 is displayed, the Java JDK is successfully installed.
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
-
- # After nginx is installed, run the command:/usr/local/nginx/sbin/nginx-t to test OK, which indicates that nginx is successfully installed.
-
- /Usr/local/nginx/sbin/nginx press enter to start nginx. You can access http: // ip/to view the default nginx page.
3. tomcat installation:
- # Download Tomcat 6.0.30 or another version from the official website:
-
- Apache-tomcat-6.0.30.tar.gz CD/usr/src & tar xzf
-
- # Directly decompress the package and run the package. Copy two Tomcat servers and name them tomcat1 tomcat2.
-
- Mv apache-Tomcat-6.0.30/usr/local/tomcat1
-
- CP/usr/local/tomcat1/usr/local/tomcat2-R
-
- # Modify the tomcat1 and tomcat2 ports respectively. The three ports must be modified as follows:
-
- Shutdown port: 8005 is mainly used to start and close.
-
- AJP port: 8009 is mainly used for AJP balancing (usually used for Apache and tomcat integration)
-
- Http port: 8080 can be directly accessed through web pages (nginx + tomcata integration)
-
- # Note * If the three port numbers of tomcat1 are 8005 8009 8080, port 8006 + 1, that is, port 8010 8081
-
- # On a server, the ports cannot be repeated; otherwise, an error is reported.
-
- # After modifying the port, start two Tomcat servers and run the following command:
-
- # How to prompt whether the file does not exist or the permission is insufficient? The bin directory of Tomcat needs to grant the execution permission to the sh file: chmod o + x *. Sh
-
- /Usr/local/tomcat1/bin/startup. Sh
-
- /Usr/local/tomcat2/bin/startup. Sh
-
- # After the instance is started, use netstat-tnl to view the six ports, that is, tomcat1 tomcat2 is successfully started. You can use http: // ip: 8080 http: // ip: 8081 to access the default Tomcat page.
# If you need to modify the Tomcat release directory as your own directory, you need to make the following adjustments to create two release directories:
- mkdir -p /usr/webapps/{www1,www2}
Edit VI/usr/local/tomcat1/CONF/server. xml and add the following content to the last line
- <Context path="" docBase="/usr/webapps/www1" reloadable="false"/>
2. Edit VI/usr/local/tomcat2/CONF/server. xml and add the following content to the line before the last
- <Context path="" docBase="/usr/webapps/www2" reloadable="false"/>
3. The release directory of tomcat1 is as follows:
-
- <body>
-
- <%=new java.util.Date()%>
- </body>
-
4. tomcat2 release directory content:
-
- <body>
-
- <%=new java.util.Date()%>
- </body>
-
Access http: // ip: 8080 and 8081 to view the test content.
5. nginx + Tomcat integration:
The integration mainly involves modifying the nginx. conf configuration and providing a complete nginx. conf online configuration. Some parameters can be modified as needed:
- User WWW;
- Worker_processes 8;
- Pid/usr/local/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 4 K;
- Large_client_header_buffers 4 32 K;
- Client_max_body_size 300 m;
- Sendfile on;
- Tcp_nopush on;
-
- Keepalive_timeout 60;
-
- Tcp_nodelay on;
- Client_body_buffer_size 512 K;
-
- Proxy_connect_timeout 5;
- Proxy_read_timeout 60;
- Proxy_send_timeout 5;
- Proxy_buffer_size 16 K;
- Proxy_buffers 4 64 K;
- Proxy_busy_buffers_size 128 K;
- Proxy_temp_file_write_size 128 K;
-
- Gzip on;
- Gzip_min_length 1 K;
- Gzip_buffers 4 16 K;
- 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 = 30 s;
- Server 127.0.0.1: 8081 Weight = 1 max_fails = 2 fail_timeout = 30 s;
- }
-
- #### Chinaapp.sinaapp.com
- Server {
- Listen 80;
- SERVER_NAME chinaapp.sinaapp.com;
- Index index. jsp index.html index.htm;
- # Release directory/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;
- }
-
- }
-
- }
# Note * The web_app defined in the server segment proxy_pass must be consistent with the web_app defined in upstream. Otherwise, the server cannot find a balance.
# In the above configuration, nginx + Tomcat Reverse Proxy Server Load balancer configuration is complete. If you want to perform dynamic/static separation, you only need to add the following configuration in nginx.
- # Configure nginx dynamic/static Separation
-
- Location ~ . * \. (HTML | HTM | GIF | JPG | JPEG | BMP | PNG | ICO | TXT | JS | CSS) $
-
- {
-
- Root/data/WWW;
-
- # Expires defines that the user's browser cache time is 3 days. If the static page is not updated frequently, you can set a longer period, which can save bandwidth and relieve server pressure.
-
- Expires 3D;
-
- }
Nginx + Tomcat server Load balancer Configuration