nginx在windows下的使用二

一、反向代理,多臺機器

1.需求和目的:nginx代理兩臺服務器,這兩臺服務器使用tomcat模擬

瀏覽器訪問http://localhost:9001/beijing/index.html,通過nginx,跳轉到一個tomcat上(端口8080),瀏覽器上顯示beijing

瀏覽器訪問http://localhost:9001/shanghai/index.html,通過nginx,跳轉到一個tomcat上(端口8081),瀏覽器上顯示shanghai

2.準備兩臺tomcat

a.已經安裝好的tomcat,端口默認就是8080

b.再下載一個解壓版的tomcat,修改端口爲8081,是修改server.xml

修改下面兩個:
<Server port="8015" shutdown="SHUTDOWN">
    
    <Connector port="8081" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443"
               maxParameterCount="1000"
               />

c.兩個tomcat準備兩個index.html:   然後把兩個tomcat分別啓動起來。

3.nginx配置:

http {
    include       mime.types;
    default_type  application/octet-stream;

    server {
        listen       9001;
        server_name  localhost;

        location ~ /beijing/ {
		    proxy_pass http://localhost:8080;
        }
		
		location ~ /shanghai/ {
		    proxy_pass http://localhost:8081;
		}
	}
}

 解釋:

location ~ /beijing
~開頭代表的是區分大小寫的一個正則匹配。

4.測試訪問:分別訪問http://localhost:9001/beijing/index.html 和 http://localhost:9001/shanghai/index.html  頁面上正確展示beijing、shanghai,測試OK

 

 

 

 

 

 

 

 

 

 

 

----

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章