0. 实现效果
通过请求访问不同路径的url实现不同的跳转功能。
例如:访问:http://62.234.149.220/edu/test.html 会跳转到 http://62.234.149.220:8001/edu/test.html。而访问:http://62.234.149.220/vod/test.html 会跳转到 http://62.234.149.220:8002/vod/test.html。
1. 准备工作
1.1 启动两台不同端口的tomcat,此处使用docker启动
docker run -d -p 8001:8080 --name tomcat01 tomcat
docker run -d -p 8002:8080 --name tomcat02 tomcat
1.2 在docker容器内部分别创建两个项目,edu和vod。并编写简单的html文件用于之后访问测试
# 进入第一个tomcat容器并创建edu项目
docker exec -it tomcat01 bash
cd webapps
mkdir edu
cd edu
touch test.html
echo "<h1>8001 tomcat<h1>" > test.html
exit
# 进入第二个tomcat容器并创建vod项目
docker exec -it tomcat02 bash
cd webapps
mkdir vod
cd vod
touch test.html
echo "<h1>8002 tomcat<h1>" > test.html
exit
1.3 在本地浏览器测试访问两个tomcat,查看是否可以正常访问
http://62.234.149.220:8001/edu/test.html
http://62.234.149.220:8002/vod/test.html
1.4 通过docker创建nginx,并启动
docker run -d -p 80:80 --name mynginx nginx
1.5 通过网页查看是否可以正常访问nginx
http://62.234.149.220
2. 具体配置
2.1 进入nginx的docker容器,并更新下载vim用于编辑文件
docker exec -it mynginx bash
apt-get update
apt-get install vim
2.2 配置nginx,实现反向代理,访问不同的路径跳转到不同的tomcat
- 进入配置的目录
cd /etc/nginx
vim 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;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
# another virtual host using mix of IP-, name-, and port-based configuration
##########################################################
server {
listen 80;
server_name 62.234.149.220;
location ~ /edu/ {
proxy_pass http://62.234.149.220:8001;
}
location ~ /vod/ {
proxy_pass http://62.234.149.220:8002;
}
}
##########################################################
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
- 退出容器后,重启nginx的docker容器
docker restart mynginx
2.3 在本地浏览器测试
http://62.234.149.220/edu/test.html => http://62.234.149.220:8001/edu/test.html
http://62.234.149.220/vod/test.html => http://62.234.149.220:8002/vod/test.html
网友评论