Nginx Implement reverse proxy switchhosts Tomacat

Source: Internet
Author: User

Nginx is a high-performance HTTP and reverse proxy server

Tool Download:

nginx:https://nginx.org/en/download.html (recommended to download stable version)

Switchhosts:https://pan.baidu.com/s/1ddj3wsi-xbo4kb3olendeq (due to the file path of the hosts is more covert, the use of switchhosts more convenient, The software mainly includes two functions: Edit hosts and switch hosts.

tomcat:https://tomcat.apache.org/download-90.cgi

The forward proxy, which is set up between the client and the target host, is used only to proxy the Internal network connection request to the Internet, the client must specify a proxy server, and the HTTP request that would otherwise be sent directly to the Web server is sent to the proxy server.

The reverse proxy server is set up on the server side to alleviate the workload of the server by buffering the frequently requested pages, forwarding the client requests to the target servers on the internal network, and returning the results from the server to the clients requesting connections on the Internet. At this point, the proxy server and the target host appear as a server externally.

Next implement the reverse proxy

To illustrate:

Domain and IP correspondence 127.0.0.1 8081.max.com 127.0.0.1 8082.max.com (step One)

Port number: 8081;8082 (step Two)

1. Use the Switchhosts tool to modify the corresponding relationship between the domain name and IP configured in the Hosts file

2. Modify the Server.xml under the Tomcat file (take the path I installed as an example)

Add a port for Tomcat: (default port number 8080)

<server port= "8005" shutdown= "shutdown" > <listener classname= " Org.apache.catalina.startup.VersionLoggerListener "/> <listener classname=" Org.apache.catalina.core.AprLifecycleListener "sslengine=" on "/> <listener classname=" Org.apache.catalina.core.JreMemoryLeakPreventionListener "/> <listener classname=" Org.apache.catalina.mbeans.GlobalResourcesLifecycleListener "/> <listener classname=" Org.apache.catalina.core.ThreadLocalLeakPreventionListener "/> <GlobalNamingResources> <resource name= "Userdatabase" auth= "Container" type= "Org.apache.catalina.UserDatabase" description= "User Databa              SE, can be updated and saved "factory=" Org.apache.catalina.users.MemoryUserDatabaseFactory " Pathname= "Conf/tomcat-users.xml"/> </GlobalNamingResources><servicename= "Catalina"> <connector port= "8080" protocol= "http/1.1" connectiontimeout= "20000" redirectport= " 8443 "/> <connectorport= "8009"Protocol= "ajp/1.3" redirectport= "8443"/> <engine name= "Catalina" defaulthost= "localhost" > <realm clas Sname= "Org.apache.catalina.realm.LockOutRealm" > <realm classname= " Org.apache.catalina.realm.UserDatabaseRealm "resourcename=" Userdatabase "/> </Realm> 

appbase= "WebApps"Unpackwars=" true "autodeploy=" true "> <valve classname=" org.apache.catalina.valves.AccessLogValve "directory=" Logs "prefix=" Localhost_access_log "suffix=". txt "pattern="%h%l%u%t "%r"%s%b "/> </Host> </Engine> </Service> <servicename= "Tomcatserver1"> <connector port= "8081" protocol= "http/1.1" connectiontimeout= "20000" redirectport= " 8443 "/> <connectorport= "8011"Protocol= "ajp/1.3" redirectport= "8443"/> <engine name= "Catalina" defaulthost= "localhost" > <realm clas Sname= "Org.apache.catalina.realm.LockOutRealm" > <realm classname= " Org.apache.catalina.realm.UserDatabaseRealm "resourcename=" Userdatabase "/> </Realm>

appbase= "WEBAPPS2"Unpackwars= "true" autodeploy= "true" > <valve classname= "org.apache.catalina.valves.AccessLogValve" directory= "Logs" prefix= "Localhost_access_log" suffix= ". txt" pattern= "%h%l%u%t"%r "%s%b"/> </Host> </Engine> </Service> <servicename= "Tomcatserver2"> <connectorport= "8082"Protocol= "http/1.1" connectiontimeout= "20000" redirectport= "8443"/> <connector port= "8012" protocol= "ajp/1.3" redirectport= "8443"/> <engine name= "Catalina" defaulthost= "localhost" > <rea LM classname= "Org.apache.catalina.realm.LockOutRealm" > <realm classname= " Org.apache.catalina.realm.UserDatabaseRealm "resourcename=" Userdatabase "/> </Realm>

appbase= "WEBAPPS3"Unpackwars= "true" autodeploy= "true" > <valve classname= "org.apache.catalina.valves.AccessLogValve" directory= "Logs" prefix= "Localhost_access_log" suffix= ". txt" pattern= "%h%l%u%t"%r "%s%b"/> </Host> </Engine> </Service></Server>

Red is the default part, Blue is the added part, and the default part of the location color is nested;

Here's the problem: we don't have webapps2,webapps3 in the Tomcat file so we need to copy WebApps

Post-Replication Status:

3. What we need to solve is how to make Nginx connect with the Tomcat server

Then you need to configure the nginx.conf, the path and my installation path as an example

#user nobody;worker_processes 1; #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid;events {worker_connections 1024;}    HTTP {include mime.types;    Default_type Application/octet-stream;    Sendfile on; Keepalive_timeout 65;upstream tomcatserver1{server 172.0.0.1:8081;} Upstream tomcatserver2{server 172.0.0.1:8082;}server {Listen 80;server_name 8081.max.com;Location/{#root html; #index index.html index.htm;Proxy_pass Http://tomcatserver1;} error_page 502 503 504/50x.html;        Location =/50x.html {root html; }} server {listen 80;server_name 8082.max.com;Location/{#root HTML1; #index index.html index.htm;Proxy_pass Http://tomcatserver2;} error_page 502 503 504/50x.html;        Location =/50x.html {root html1; }     }}

Nginx Implement reverse proxy switchhosts Tomacat

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.