Windows nginx + Tomcat load Integration

Source: Internet
Author: User
Tags epoll

I'm not talking much, so I went straight to the topic.

Download complete instance

Step 1:

Go to the official download of a Windows version of nginx1.5 is now the latest version, and then casually decompress to a directory, my directory is c: \ Program Files (x86) \ nginx-1.5.0

Then, double-click nginx.exe to start. The default configuration is port 80. If there are no otherProgramThere should be no problem when using this port, and then access http: // localhost directly. The welcome interface is OK.

Step 2:

Decompress two installation-free Tomcat files to any directory. My directory is E :\

Use myeclipse or eclipse to create a web project and place it on demand.

Index. jsp content

<% @ Page Language = "Java" contenttype = "text/html; charset = UTF-8" pageencoding = "UTF-8" %> <! Doctype HTML public "-// W3C // dtd xhtml 1.0 strict // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <HTML xmlns = "http://www.w3.org/1999/xhtml"> 

Then modify the server. xml of our two Tomcat servers to point to our project and modify the startup port. After all, the port of a computer is unique, so we can only modify different ports to avoid conflict.

This is an example. You can search for the port directly. If there is a port number, the rule is changed. Add 1 to the front and then add 1 to the number (for example, the port number is 8080, you can change it to 18080 + 1 = 18081 ).

<? XML version = '1. 0' encoding = 'utf-8'?> <Server port = "18005" shutdown = "shutdown"> <! -- APR library loader. Documentation At/docs/apr.html --> <listener classname = "org. Apache. Catalina. Core. aprlifecyclelistener" sslengine = "on"/> <! -- Initialize Jasper prior to webapps are loaded. Documentation At/docs/jasper-howto.html --> <listener classname = "org. Apache. Catalina. Core. jasperlistener"/> <! -- Prevent memory leaks due to use of particle Java/javax APIs --> <listener classname = "org. Apache. Catalina. Core. jrememoryleakpreventionlistener"/> <! -- JMX support for the Tomcat server. documentation At/docs/non-existent.html --> <listener classname = "org. apache. catalina. mbeans. serverlifecyclelistener "/> <listener classname =" org. apache. catalina. mbeans. globalresourceslifecyclelistener "/> <globalnamingresources> <Resource Name =" userdatabase "auth =" Container "type =" org. apache. catalina. userdatabase "Description =" user database that can be updated And saved "factory =" org. apache. catalina. users. memoryuserdatabasefactory "pathname =" CONF/tomcat-users.xml "/> </globalnamingresources> <service name =" Catalina "> <connector Port =" 18080 "protocol =" HTTP/1.1 "connectiontimeout =" 20000 "redirectport =" 18443 "/> <! -- Define an AJP 1.3 connector on port 8009 --> <connector Port = "18009" protocol = "AJP/1.3" redirectport = "18443"/> <engine name = "Catalina" defaulthost = "localhost" jvmroute = "cmdat1"> <realm classname = "org. apache. catalina. realm. userdatabaserealm "resourcename =" userdatabase "/> 

Start the two Tomcat servers and check whether there is a problem with the wood. If there is a problem with the wood, it will be OK (start through the start below the bin)

Step 3: Modify the nginx configuration file.

Add a proxy under Conf. conf and gzip. CONF file. The former is required by the proxy, and the latter is required by gzip compression (Optional). Find the specific parameters for reference. it's not too long.

Proxy. conf

 
Proxy_redirect off; proxy_set_header host $ host; proxy_set_header X-real-IP $ remote_addr; proxy_set_header X-forwarded-for $ scheme; limit 10 m; Limit 128 K; Limit 300; Limit 300; proxy_read_time out 300; proxy_buffer_size 4 K; proxy_buffers 4 32 K; proxy_busy_buffers_size 64 K; proxy_temp_file_write_size 64 K;

Gzip. conf

Gzip on; gzip_min_length 1024; gzip_buffers 4 8 K; gzip_comp_level 9; gzip_proxied any; gzip_types application/XML application/JavaScript Application/X-JavaScript Application/atom + XML application/RSS + XML; gzip_types text/CSS text/HTML text/JavaScript text/JS text/plain text/XML;

The last step is to modify nginx. conf.

You can copy the following configuration to replace the default configuration, and then adjust it as needed.

# Users and groups used by nginx, not specified in the window # user niumd; # Number of working sub-processes (usually equal to the number of CPUs or twice the CPU) worker_processes 2; # error log storage path # error_log logs/error. log; # error_log logs/error. log notice; error_log logs/error. log Info; # specify the PID to store the file PID logs/nginx. PID; events {# epoll is recommended for Linux using the network I/O model. kqueue is recommended for FreeBSD and is not specified for Windows. # Use epoll; # maximum number of connections allowed worker_connections 2048;} HTTP {include mime. types; default_type application/octet-stream; # define log format # 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 off; access_log logs/access. log; client_header_timeout 3 m; client_body_timeout 3 m; send_timeout 3 m; client_header_bu Ffer_size 1 K; large_client_header_buffers 4 4 4 K; sendfile on; tcp_nopush on; tcp_nodelay on; # keepalive_timeout 75 20; Include gzip. conf; include proxy. conf; upstream localhost {# allocate requests to the backend Tomcat Based on IP computing. Many people mistakenly believe that the session problem can be solved. # The IP address may be different during route switching on the same machine over multiple networks # ip_hash; # The weigth parameter indicates the weight. The higher the weight, the higher the probability of being allocated to the server localhost: 18080 Weight = 1; server localhost: 28080 Weight = 6;} server {Listen 80; SERVER_NAME localhost; Location/{root E:/Eclipse/workspace/nginx/webroot/statict; index index.html index.htm;} location ~ \. (HTML | JS | CSS | PNG | GIF) $ {root E:/Eclipse/workspace/nginx/webroot/statict;} location ~ \. (JSP | action) $ {proxy_connect_timeout 3; proxy_send_timeout 30; proxy_read_timeout 30; proxy_pass http: // localhost ;}}}

The Root parameter points to the folder path. For example, my default homepage is placed in

 
E:/Eclipse/workspace/nginx/webroot/statict

Indexis the various types of static files under the root folder. In this example, index.html is configured. When I request http: // localhost, I will request index.html under the root folder.

 
Location ~ \. (HTML | JS | CSS | PNG | GIF) $

Here, regular expression filtering will find the file in the root path when it comes to the type in the brackets. Obviously, this is the dynamic and static separation method. I put all static files in static.

 
Location ~ \. (JSP | action) $

Here, the JSP and action requests are filtered and transferred to Tomcat to request resources.

Finally, save the configuration file, disable nginx, restart, and access htt: // localhost/nginx/index. jsp.

After several requests, we can see that two Tomcat's small black windows show output items.

The nginx + Tomcat load is almost enough. The specific configuration details are as follows: for example, to reject the requests from Shenma, and to request the conversion path of Shenma, write several more location configurations, study these contents on your own!

Related Article

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.