Nginx+tomcat cluster configuration (4)--rewrite rules and multi-application root setup Ideas

Source: Internet
Author: User
Tags app service root access

Objective:
There is a very important concept in Nginx, which is the rewrite rule. It modifies the URL and then makes internal redirects. Rewrite granted Nginx more freedom, making the post-service access more local.
This article will briefly describe the rewrite rules and principles of nginx, and explain how to use rewrite to realize the multi-domain root access of single Tomcat instance/multi-web App service.

Objective of the case:
First, let's talk about a scenario where multiple Web App services correspond to multi-domain root access.
  
Note: A domain name corresponds to a Tomcat instance, and a Tomcat instance contains only one webapp.
Specifically, the blog post: "Nginx+tomcat cluster configuration (1)---root settings and multi-backend distribution configuration."
This architectural model is also a way for big companies, after all, big companies are not short of machine resources, and there is a legitimate reason: isolation and non-impact.
For small companies/startups, machine resources are not plentiful, and if a Tomcat instance deploys only one web app, it's a little wasteful. Deploying multiple Web apps, the mapping of domain names is another wave, because in Tomcat, the URI that WebApp accesses needs to add an app name. This does not conform to the root directory access settings.
Of course, Nginx's powerful rewrite rules can help us solve the problem, and the framework for its eventual effect is as follows:
  

Rewrite Rule description:
Nginx's rewrite rule is used for URL rewriting, which is consistent with Perl's regular expression.
The rules of Rewrite are as follows:

Rewrite regexp replacement [flag]

The flag tag values are as follows:

Last: Equivalent to Apache Reed (L) mark, indicating completion rewrite;break; When the rule match is complete, the match is terminated and no longer matches the following rule redirect: Returns 302 temporary redirect, The browser address will show the URL address after the jump permanent: return 301 Permanent Redirect, the browser address bar will show the URL address after the jump

Note: Last and break are used to implement URL rewriting, browser address bar URL address unchanged
Simple rules:

Rewrite ^/search/(. *) $/search.php?q=$1;

The URI before the input is:

Http://website.com/search/some-search-keywords

Then the rewritten URI is:

Http://website.com/search.php?q=some-search-keywords

The specific can refer to the blog: "Nginx rewrite rules preliminary study." This is no longer a specific elaboration.

Location rules:
Location Syntax:

syntax:location [=|~|~*|^~|@]/uri/{...}

Location involves two large chunks of normal and regular matches, and we'll start by distinguishing
Common match

= Exact match ^~ prefix match

Note: For ' ^~ ', when matched with the default prefix match, it executes immediately and is not overwritten by subsequent regular matches. For a normal match, the maximum prefix is whichever.
Regular Match

~ Case-sensitive regular match ~* case-insensitive regular match

Note: The regular match is scheduled in edit order.
can be specific reference blog: nginx location matching rules detailed.
Focus on a famous saying: The regular location matches the strict exact match result of the common location, but the maximum prefix matching result of the common location is overwritten.

Experimental deployment:
For example, if you create a web app and deploy it to Tomcat (multi-application), the default rule is to add a prefix to the project name in the URI to access it.
For example, we assume that the project is named shopping, giving it a separate domain name: shopping.website.com.
Then the default URL needs shopping.website.com/shopping to be OK, now it needs to be set to shopping.website.com.
How to use Nginx configuration, to achieve our goal?
In the nginx.conf configuration, add the rewrite rule:
    if ($uri!~/shopping/) {        rewrite ^ (. *) $/shopping/$1 last;    }    location/shopping/{        proxypass <<tomcat server>>    }
    Note: Rewrite may cause recursion (Nginx blocks infinite recursion, up to 10 times).
Simply by this modification, we have achieved our intended goal.
Additional Benefits:
As you know, the root project is Tomcat's backend management service, which, as previously done, would be sacrificed to shut down the service. If you introduce rewrite, you can keep the service.

Postscript:
has been to the nginx not enough comprehensive and systematic, today by this experiment to strengthen. The right to take notes, with June encouragement.

Public Number & Games sites:
Personal public Number: Wooden purpose H5 game world

  

Personal Game Folio site (still under construction ...): www.mmxfgame.com, also direct IP access : http://120.26.221.54/. 

Nginx+tomcat cluster configuration (4)--rewrite rules and multi-application root setup Ideas

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.