1. 程式人生 > >nginx實現反向代理 switchhosts tomacat

nginx實現反向代理 switchhosts tomacat

logger ica connector notice scrip pan down script 返回

Nginx 是一個高性能的HTTP和反向代理服務器

工具下載:

nginx下載地址:https://nginx.org/en/download.html (建議下載穩定版)

switchhosts下載地址:https://pan.baidu.com/s/1ddj3WSi-XBO4KB3olEnDEQ(由於hosts的文件路徑比較隱蔽,使用switchhosts更加便捷,該軟件主要帶有兩個功能:編輯hosts和切換hosts)

tomcat下載地址:https://tomcat.apache.org/download-90.cgi

正向代理,架設在客戶機與目標主機之間,只用於代理內部網絡對Internet的連接請求,客戶機必須指定代理服務器,並將本來要直接發送到Web服務器上的http請求發送到代理服務器中。

技術分享圖片

反向代理服務器架設在服務器端,通過緩沖經常被請求的頁面來緩解服務器的工作量,將客戶機請求轉發給內部網絡上的目標服務器;並將從服務器上得到的結果返回給Internet上請求連接的客戶端,此時代理服務器與目標主機一起對外表現為一個服務器。

技術分享圖片

接下來實現反向代理

舉例說明:

域名和ip的對應 127.0.0.1 8081.max.com 127.0.0.1 8082.max.com(步驟一)

端口號:8081;8082(步驟二)

1.使用switchhosts工具修改hosts文件中配置的域名和ip的對應關系

技術分享圖片

2.修改tomcat文件下的server.xml(以我安裝的路徑為例)

技術分享圖片

為tomcat添加端口:(默認端口號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 database that can be updated and saved"
              factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
              pathname="conf/tomcat-users.xml" />
  </GlobalNamingResources>
  <Service name="Catalina"
> <Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" /> <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" /> <Engine name="Catalina" defaultHost="localhost"> <Realm className="org.apache.catalina.realm.LockOutRealm"> <Realm className="org.apache.catalina.realm.UserDatabaseRealm" resourceName="UserDatabase"/> </Realm> <Host name="localhost" 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>
<Service name="tomcatserver1"> <Connector port="8081" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" /> <Connector port="8011" protocol="AJP/1.3" redirectPort="8443" /> <Engine name="Catalina" defaultHost="localhost"> <Realm className="org.apache.catalina.realm.LockOutRealm"> <Realm className="org.apache.catalina.realm.UserDatabaseRealm" resourceName="UserDatabase"/> </Realm> <Host name="localhost" 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> <Service name="tomcatserver2"> <Connector port="8082" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" /> <Connector port="8012" protocol="AJP/1.3" redirectPort="8443" /> <Engine name="Catalina" defaultHost="localhost"> <Realm className="org.apache.catalina.realm.LockOutRealm"> <Realm className="org.apache.catalina.realm.UserDatabaseRealm" resourceName="UserDatabase"/> </Realm> <Host name="localhost" 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>

紅色為默認部分,藍色為添加部分,區分默認部分的位置顏色嵌套;

問題來了:我們tomcat文件中並沒有webapps2,webapps3那麽需要我們復制webapps

復制後的狀態:

技術分享圖片

3.我們需要解決的是如何使nginx與tomcat服務器進行連接

那麽就需要配置nginx.conf,路徑與我的安裝路徑為例

技術分享圖片

#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   500 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   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html1;
        } 
    }
}

nginx實現反向代理 switchhosts tomacat