Nginx+tomcat Implement reverse proxy __nginx

Source: Internet
Author: User
Tags nginx server nginx reverse proxy

The usual proxy server, which is used only for proxy internal network connection requests to the Internet, the client must specify a proxy server and send the HTTP request that was intended to be sent directly to the Web server to the proxy server to initiate a request to the Web server on the Internet by the proxy server, Finally reach the client's purpose of Internet access.

The reverse proxy (Reverse proxy) means to accept a connection request on the Internet with a proxy server, then forward the request to a server on the internal network and return the results obtained from the server to the client requesting the connection on the Internet, At this point, the proxy server behaves as a reverse proxy server. As shown in the following illustration:



Figure A reverse proxy server


Let's take a look at how to implement a reverse proxy using Nginx+tomcat.


first, the realization of the scene

Two tomcat servers via Nginx reverse proxy, this example is implemented with two virtual machines, one virtual machine provides ngxin service, and the other virtual machine provides two Tomcat services.

Nginx Load Balancer: 192.168.0.103

tomcat1:192.168.0.106:8080

tomcat2:192.168.0.106:8081

When configured, start Tomcat

Figure II Tomcat+nginx implementation of load Balancing configuration


Second, configure the host file


We visit Tomcat running on different ports by accessing different domain names, which requires configuring our host file;

Www.test1.com access to Tomcat running on port 8080

Www.test2.com access to Tomcat running on port 8081

Add the following domain name configuration in the C:\Windows\System32\drivers\etc\hosts file:

192.168.0.106 www.test1.com
192.168.0.106 www.test2.com



Third, Nginx reverse proxy configuration


According to our configuration above, the reverse proxy is configured in the nginx.conf file as follows:

#配置一个代理即tomcat1服务器
        upstream Tomcat_server1 {
            server 192.168.0.106:8080;
        }
		
#配置一个代理即tomcat2服务器
	upstream Tomcat_server1 {
            server 192.168.0.106:8081;
        }
		
#配置一个虚拟主机
		server {
			listen;
			server_name www.test1.com;
			Location/{
					#域名www. test1.com requests are forwarded to Tomcat_server1 TOMCAT1 service
					proxy_pass http://tomcat_server1;
					#欢迎页面, find the page
					index index.jsp index.html index.htm
			}
		
		in Left-to-right order server {
			listen Bayi;
			server_name bbb.test.com;
			Location/{
				 #域名www. test2.com requests are forwarded to Tomcat_server2 TOMCAT2 service
                  proxy_pass http://tomcat_server2;
                  Index index.jsp index.html index.htm
			}
		}

Iv. Modifying the access page for Tomcat


Modify the contents of the webapps/root/index.jsp under two Tomcat respectively, using the TOMCAT1 and TOMCAT2 two services first to display different content, as follows:

TOMCAT1 under the index.jsp modified:

The index interface of figure three TOMCAT1

TOMCAT2 under the index.jsp modified:


Figure Four the index interface of TOMCAT2


Five, Test


Start the Nginx server, respectively, to access the www.test1.com and www.test2.com test the reverse proxy.







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.