Nginx 反向代理 tomcat 直接轉發 與 正則匹配

實例一:html

經過ningx1080端口轉發 到tomcat8080nginx

1. 安裝tomcat ,啓動,可以訪問8080web

2. nginx 配置  正則表達式

 proxy_pass http://127.0.0.1:8080;apache

server {
        listen      1080;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
            proxy_pass http://127.0.0.1:8080;
	}

3. 雲服務器防火牆打開1080端口,而且安全組開放1080centos

firewall-cmd  --permanent --zone=public --add-port=1080/tcp

 4. 啓動nginxtomcat

 5. 訪問nginx 1080 便可訪問tomcat 的8080 端口安全

實例2:服務器

根據訪問的路徑跳轉到不一樣的端口中。app

1. 準備tomcat8081

建立文件夾 /usr/local/tomcat8081/ 複製進壓縮包解壓

[root@VM-0-7-centos apache-tomcat-7.0.106]# pwd
/usr/local/tomcat8081/apache-tomcat-7.0.106

2. 修改 conf/server.xml

Server.port 8015

Server.Connetor.port:8081

<Server port="8015" shutdown="SHUTDOWN">
....


    <Connector port="8081" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />

3.進入bin 啓動 ./startup.sh

4 tomcat 8080中 建立頁面 edu/index.html

4.1 建立目錄

/usr/local/tomcat/apache-tomcat-7.0.106/webapps/edu

4.2 上傳index.HTML

html內容爲

8080!!!
[root@VM-0-7-centos edu]# ll
total 4
-rw-r--r-- 1 root root 7 Nov 17 11:26 index.html

4.3 測試訪問

[root@VM-0-7-centos edu]# curl localhost:8080/edu/index.html
8080!!!

5. tomcat 8081 中建立目錄 並上傳文件

[root@VM-0-7-centos webapps]# pwd
/usr/local/tomcat8081/apache-tomcat-7.0.106/webapps
[root@VM-0-7-centos webapps]# mkdir vod
[root@VM-0-7-centos webapps]# cd vod
[root@VM-0-7-centos vod]# pwd
/usr/local/tomcat8081/apache-tomcat-7.0.106/webapps/vod
[root@VM-0-7-centos vod]# rz

[root@VM-0-7-centos vod]# curl localhost:8081/vod/index.html
8081!!!

6. 配置nginx.conf

/usr/local/nginx/conf/nginx.conf

server {
        listen  9001;
        server_name 127.0.0.1;
        location ~ /edu/ {
                proxy_pass http://127.0.0.1:8080;
        }

        location ~ /vod/ {
                proxy_pass http://127.0.0.1:8081;
        }
    }

7. 測試

curl 127.0.0.1:9001/edu/
8080!!!
curl 127.0.0.1:9001/vod/
8081!!!

8.location 說明

location 指令說明
該指令用於匹配 URL。
語法以下:
 
location [ = | ~ | ~* | ^~ ] uri {

}

 

一、= :用於不含正則表達式的 uri 前,要求請求字符串與 uri 嚴格匹配,若是匹配
成功,就中止繼續向下搜索並當即處理該請求。
二、~:用於表示 uri 包含正則表達式,而且區分大小寫。
三、~*:用於表示 uri 包含正則表達式,而且不區分大小寫。
四、^~:用於不含正則表達式的 uri 前,要求 Nginx 服務器找到標識 uri 和請求字 符串匹配度最高的 location 後,當即使用此 location 處理請求,而再也不使用 location 塊中的正則 uri 和請求字符串作匹配。
注意:若是 uri 包含正則表達式,則必需要有 ~ 或者 ~* 標識。
相關文章
相關標籤/搜索