First, we will discuss why nginx: 1. Similar to apache + resin, nginx is used to provide static page services, which is better than java servers. Although the performance of these java servers is good, the new version of tomcat even supports epoll, but using nginx to process static files must be more secure and faster than these servers. 2. nginx works with ja
First, we will discuss why nginx should be used:
1. Similar to apache + resin, nginx is used to provide static page services, which is better than java Server. Although the performance of these java servers is not good,McThe new at version even supports epoll, but using nginx to process static files must be more secure and faster than these servers.
2. nginx has a lower coupling degree than apache in combination with java application servers, so it can connect to all java application servers that support http service in the same way, whether it is tomCat, Resin, weblogic, jetty, websphere ......, In addition, an application can be easily connected to different servers at the same time. For example, one running tomcat and one running resin are very useful in switching or testing the production environment. In addition, the reduction of coupling degree will minimize the mutual influence between nginx and these servers, so that the two can be held accountable separately. apache generally uses a module to deal with the java application server, this module will have more or less an impact on apache running. nginx tells us how powerful these modules are, and the java server will be better developed in terms of fastcgi protocol support.
3. Some nginx features can save the fragile java Server. Which can be named: 1 is customizableServer Load balancerPolicy; 2 is the Super fault shielding function that can avoid missing any request; 3 is the asynchronous processing of client connections; 4 is a times load capacity better than the java Server.
Based on these advantages, the general java application server, connected to an nginx, can be comparable to the large java with tens of thousands of dollars in service stability.ClusterServer, java developers do not have to worry about java performance.
If you decide to try nginx, the next thing is much easier than spitting.
Nginx can be connected to the java background in two ways:
1. Send jsp extension requests to the backend. This is a traditional method.
2. Send all requests to the background, and leave the directories of some static files
3. Send all requests to the backend, and leave a link with a certain extension
These three methods use link features for processing. For example, the server is a single machine ):
First configure a upsTrEam, which is common:
Upstream www.SuDone.com {
Server 127.0.0.1: 8081;
Server 127.0.0.1: 8082;
}
127.0.0.1: 8081 and 127.0.0.1: 8082 are backend servers. resin supports two types of services. One is the engine method used to connect apache and IIS, and the other is the http server method, you must start the http server to make these two ports valid. I am not quite sure about tomcat. If there are two methods, you must also start the http server method. The check method is to access the page directly through a link.
Policy Configuration for extension:
Server {
Listen 80;
Server_name www.sudone.com;
Location /{
Root/data/html /;
ReWrite^/(%0-9%%%%.html $/a. jsp?Id= $1 last;
}
Location ~ (. Jsp) | (. do) $ {
Proxy_pass http://www.sudone.com;
}
}
Note:
1. location ~ (. Jsp) | (. do) $ do not write/or write more/jsp after the proxy_pass label in this section. For example, it cannot be written as proxy_pass http://www.sudone.com/#. the original reason is that if locationis usedRegular ExpressionExpression, the link path cannot be included in proxy_pass.
2. server_name www.sudone.com; specifies the domain name used by the user to access nginx, proxy_pass http://www.sudone.com; this is the name of nginx upstream and the domain name used to access the background of nginx, in most cases, you can use the same name. In some special cases, you need to use different names, so make sure that the background does not cause any confusion.
3. rewrite can be written directly under location/. It cannot be written in location ~ (. Jsp) | (. do) $. Note that the parameter after rewrite must be last here.
4. I only configured jsp and do in this configuration. Of course, java not only supports so many, so you need to find all java link rules.
Send all requests to the backend configuration (2 and 3 are written together ):
Server {
Listen 80;
Server_name www.sudone.com;
Location /{
Proxy_pass http://www.sudone.com;
}
Location/images /{
Alias/Data/html/images /;
}
Location ~ (.Jpg) | (.gif) | (.png) $
{
Root/data/html /;
}
}
The two configurations and the principles for jsp extensions are exactly the opposite. Note:
1. Unlike jsp extensions, the proxy_pass http://www.sudone.com; here, because it is not usedRegular ExpressionSo you can write/or support/jsp later.
2. The location/images/section can be clearly viewed by alias, or the root tag can be used. However, it should be written as root/data/html/, level 1 in advance.
3. location ~ (.Jpg) | (.gif) | (.png) $ this section can only use the root label, but cannot write alias. If location uses regular expressions and then uses alias, nginx does not support this section, I also don't know what the logic will be like if I use alias.
The above configuration is the most simplified configuration, and many parameters can be adjusted. I will discuss it in other articles. In addition, we recommend that you test the configuration well. If you have any problems, you should first think about it. These configurations are not omnipotent and cannot solve all the problems. At the same time, these configurations do not have advantages or disadvantages. If you are not sure which one to use, use the first method.