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 包含正则表达式,则必需要有 ~ 或者 ~* 标识。
相关文章
相关标签/搜索