文章目录
1. 环境
ubuntu服务器
nginx 版本: nginx/1.13.7
2. 想要效果描述(知道自己要干什么)
url访问过程中,同一个ip和端口,其中携带的关键字不同,代理到的服务器不同。
比如:
访问一:http://ip/apa1/a.html地址,经过反向代理服务器代理到tomcat8080上。
访问二:http://ip/apa2/a.html地址,经过反向代理服务器代理到tomcat8081上。
3. 准备(知道自己需要什么)
- 需要nginx服务
- nginx配置需要改为反向代理(代理的是:监听ip和端口进行代理到不同tomcat上)
- 俩个tomcat服务,8080和8081
- 每个tomcat下webapp文件夹,tomcat 8080新建apa1和tomcat 8081新建apa2
- 在apa1和apa2文件夹下新建文件a.html
4. 操作(重要文件需要备份)
4.1 启动nginx服务
# 启动nginx服务
nginx
4.2 修改nginx配置文件
# 1. 备份配置文件
cp nginx.conf nginx.conf.back
# 2. 修改配置文件
vim nginx.conf
配置文件部分内容修改
server {
listen 80;
server_name localhost;
location ~ /apa1/ {
root html;
proxy_pass http://localhost:8080;
index index.html index.htm;
}
location ~ /apa2/ {
root html;
proxy_pass http://localhost:8081;
index index.html index.htm;
}
}
保存,退出
重置配置文件,让配置文件生效
nginx -s reload
location / {}中的"/"解释:
/:代表匹配的路径,如果路径前边有修饰符,则匹配规则不同。
1、=:用于不含正则表达式的uri前,要求请求字符串与uri严格匹配,如果匹配成功,就停止继续向下搜索并立即处理该请求。
2、~:用于表示包含正则表达式,并且区分大小写。
3、~*:用于表示包含正则表达式,并且不区分大小写。
4、^~:用于不含正则表达式的uri钱,要求nginx服务器找到标识uri和请求字符串匹配度最高的location后,立即使用此location处理请求,而不再使用location块中的正则uri和请求字符串做匹配。
注意:如果uri包含正则表达式,则必须要有或者*标识。
4.3 新建资源a.thml
- tomcat 8080下webapps中,新建文件夹apa1
mkdir apa1
tomcat 8081下webapps中,新建文件夹apa2
mkdir apa2
- 在apa1和apa2文件夹下,分别新建文件a.html
# 新建a.html
vim a.html
a.html内容为:
# tomcat 8080下a.html内容为
<h1>tomcat 8080</h1>
# tomcat 8081下a.html内容为
<h1>tomcat 8081</h1>
5. 验证(与预期是否符合)
访问:http://xxxxx.top/apa1/a.html
访问:http://xxxxx.top/apa2/a.html