Categories
Uncategorized

Nginx summary (five) and how to configure nginx reverse proxy to achieve tomcat

Speaking in front of how to configure Nginx virtual host, you can go here to see nginx series: https: //www.cnblogs.com/zhangweizhong/category/1529997.html

I say today is how to configure nginx and tomcat achieve reverse proxy.

Note: Because this article is nginx in a series of articles, article there are many other configurations, may have said previous article talked about, then there would be no follow-up in the introduction, did not say if some configuration occurs, you may need to to see the previous article.

 

What is a reverse proxy

The proxy server is usually only used for proxy internal network connection requests to the Internet, the client must specify the proxy server, and would have to be sent directly to http requests on the Web server sends to a web proxy server on the Internet by the proxy server server-initiated requests, and ultimately to the client access to the Internet. This is known as forward proxy, usually directly called agent.

Reverse proxy (Reverse Proxy) refers to the proxy server to accept connection requests on the internet, and then forwards the request to the server on the internal network, and the results obtained from the server back to the requesting client on the internet connection, At this point the external proxy server on the performance of a reverse proxy server.

Nginx only request forwarding, multiple back-end http server to provide services, the role of nginx is to put forward the request to the back of the server, forwards the request to decide who handles.

As shown below:

 

 

Nginx reverse proxy configuration

Scenarios

Tomcat services through two reverse proxy nginx, this example uses three virtual machines for testing,

nginx server: 192.168.78.132

tomcat1 server: 192.168.78.134

tomcat2 server: 192.168.78.135

As shown below:

 

 

Preparing the Environment

1. Two tomcat server, I used here is to use apache-tomcat-7.0.57 version, start tomcat on 192.168.78.134 and 192.168.78.135 virtual machine.

2. Nginx server, the previously installed, ip address is: 192.168.78.132

3. Modify the contents of the webapps two tomcat / ROOT / index.jsp, the use of two services tomcat1 and tomcat2 Home display different content.

4. Specify aaa.test.com and bbb.test.com correspond 192.168.78.132 virtual machine host file: Modify window hosts file: (C: \ Windows \ System32 \ drivers \ etc)

192.168.78.132 aaa.test.com
192.168.78.132 bbb.test.com

 

nginx reverse proxy configuration

According to your needs in the top of the file nginx.conf reverse proxy, as follows:

#配置一个代理即tomcat1服务器
upstream tomcat_server1 {
    server 192.168.78.134:8080;
}

#配置一个代理即tomcat2服务器
upstream tomcat_server2 {
    server 192.168.78.135:8080;
}

#配置一个虚拟主机
server {
    listen 80;
    server_name aaa.test.com;
    location / {
        #域名aaa.test.com的请求全部转发到tomcat_server1即tomcat1服务上
        proxy_pass http://tomcat_server1;

        #欢迎页面,按照从左到右的顺序查找页面
        index index.jsp index.html index.htm;

    }
}

server {
    listen 80;
    server_name bbb.test.com;
    location / {
        #域名bbb.test.com的请求全部转发到tomcat_server2即tomcat2服务上
        proxy_pass http://tomcat_server2;

        index index.jsp index.html index.htm;
    }
}

 

test

Separate visits aaa.test.com, bbb.test.com test reverse proxy.

 

As can be seen from the above screenshot:

Requesting access aaa.test.com by nginx proxy access tomcat1,

Requesting access bbb.test.com by nginx proxy access tomcat2.

 

At last

Or more, put nginx and how to configure tomcat achieve reverse proxy finished. While more steps, but the operation is relatively simple.

 

Leave a Reply