前言
nginx的80端口服务可以配置多域名转发,满足我们单个/单台nginx实现配置多个域名转发功能,不需要每个/每台服务部署nginx的80服务,注:nginx的443端口服务同理
使用
- 1.关闭防火墙(firewalld,iptables)
- 2.关闭selinux
- 3.安装docker
- 4.安装docker-compose工具
如上不再赘述
部署一个web服务,页面展示nature
- 配置web服务首页
mkdir -p /opt/nginx/nature/html
cat > /opt/nginx/nature/html/index.html <<-EOF
nature
EOF
- docker部署web服务
mkdir -p /data/nginx/nature
cat > /data/nginx/nature/docker-compose.yml <<-EOF
version: "3.5"
services:
mysql:
image: nginx
container_name: nginx_nature
hostname: nginx_nature
privileged: true
ports:
- 8080:80
environment:
- SET_CONTAINER_TIMEZONE=true
- CONTAINER_TIMEZONE=Asia/Shanghai
volumes:
- /etc/localtime:/etc/localtime:ro
- /opt/nginx/nature/html:/usr/share/nginx/html:Z
restart: always
tty: true
EOF
cd /data/nginx/nature && docker-compose up -d
- 查看首页
nature]# docker ps | grep nature
bcb0462de867 nginx "/docker-entrypoint.…" 12 minutes ago Up 12 minutes 0.0.0.0:8080->80/tcp nginx_nature
nature]# curl localhost:8080
nature
注: 通过服务器访问,也可以通过浏览器访问,请自行选择访问方式
部署另一个web服务,页面展示confirm
- 配置web服务首页
mkdir -p /opt/nginx/confirm/html
cat > /opt/nginx/confirm/html/index.html <<-EOF
confirm
EOF
- docker部署web服务
mkdir -p /data/nginx/confirm
cat > /data/nginx/confirm/docker-compose.yml <<-EOF
version: "3.5"
services:
mysql:
image: nginx
container_name: nginx_confirm
hostname: nginx_confirm
privileged: true
ports:
- 9080:80
environment:
- SET_CONTAINER_TIMEZONE=true
- CONTAINER_TIMEZONE=Asia/Shanghai
volumes:
- /etc/localtime:/etc/localtime:ro
- /opt/nginx/confirm/html:/usr/share/nginx/html:Z
restart: always
tty: true
EOF
cd /data/nginx/confirm && docker-compose up -d
- 查看首页
nature]# docker ps | grep confirm
2b47b9bedd8d nginx "/docker-entrypoint.…" 3 days ago Up 3 days 0.0.0.0:9080->80/tcp nginx_confirm
nature]# curl localhost:9080
confirm
注: 通过服务器访问,也可以通过浏览器访问,请自行选择访问方式
配置nginx的80端口服务转发多域名
- 配置域名转发
mkdir -p /data/nginx/master
cat > /data/nginx/master/nginx.conf <<-EOF
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 65535;
}
http {
include /etc/nginx/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 /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name www.nature.com;
location / {
proxy_pass http://172.21.0.14:8080;
}
}
server {
listen 80;
server_name www.confirm.com;
location / {
proxy_pass http://172.21.0.14:9080;
}
}
}
EOF
- docker部署80端口的nginx容器服务
cat > /data/nginx/master/docker-compose.yml <<-EOF
version: "3.5"
services:
mysql:
image: nginx
container_name: nginx_master
hostname: nginx_master
ports:
- 80:80
- 443:443
environment:
- SET_CONTAINER_TIMEZONE=true
- CONTAINER_TIMEZONE=Asia/Shanghai
volumes:
- /etc/localtime:/etc/localtime:ro
- /data/nginx/master/nginx.conf:/etc/nginx/nginx.conf
restart: always
tty: true
EOF
cd /data/nginx/master && docker-compose up -d
- 查看80端口的容器服务
master]# docker ps | grep nginx_master
5b01358a12a8 nginx "/docker-entrypoint.…" 2 days ago Up 2 days 0.0.0.0:80->80/tcp, 0.0.0.0:443->443/tcp nginx_master
- 配置本机域名解析
cat >> /etc/hosts <<-EOF
172.21.0.14 www.nature.com
172.21.0.14 www.confirm.com
EOF
- 访问两个web服务
master]# curl www.nature.com
nature
master]# curl www.confirm.com
confirm
注: 通过服务器访问,也可以通过浏览器访问(此方式需要搭建内网dns,并把相应的ip及域名添加到域名解析中),请自行选择访问方式
结语
… …