场景: 一台服务上部署了n多个网站,假设 有 web1 web2 ,服务器的IP 是 192.168.1.10 ,web1 的访问地址 192.168.1.10:8080 ,web2 的访问地址 192.168.1.10:8081, 有一个域名 myhost.com , 当然你会绑定 IP myhost.com -> 192.168.1.10 这样,你就可以 myhost.com:8080 访问 web1 , myhost.com:8081 访问 web2 , 当然没有任何问题,
怎么才能不加端口号 也能访问不同的网站能, 答案肯定是没有问题的,可以解决.
使用不同的域名区分不同的网站,使用 nginx 代理
web1.myhost.com -> 192.168.1.10:8080
web2.myhost.com -> 192.168.1.10:8081
1. 拉取镜像
docker pull nginx
2. 获取配置文件
从镜像中获取
docker run --name tmp-nginx-container -d nginx mkdir /etc/nginx docker cp tmp-nginx-container:/etc/nginx/nginx.conf /etc/nginx/ docker stop tmp-nginx-container docker rm tmp-nginx-container
3. 编辑配置文件
创建一配置文件
/etc/nginx/hosts.conf
server { listen 80; listen [::]:80; server_name web1.myhost.com; location / { proxy_pass http://192.168.1.10:8080/; } error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } } server { listen 80; listen [::]:80; server_name web2.myhost.com; location / { proxy_pass http://192.168.1.10:8081/; } error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } }
修改 nginx.conf 指向 hosts.conf
nano /etc/nginx/nginx.conf default_type application/octet-stream; user nginx; worker_processes auto; error_log /var/log/nginx/error.log notice; pid /var/run/nginx.pid; events { worker_connections 1024; } 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; include /etc/nginx/hosts.conf; // 添加自定义配置 include /etc/nginx/conf.d/*.conf; }
4. 运行
docker run --name nginx -p 80:80 -v /etc/nginx/hosts.conf:/etc/nginx/hosts.conf:ro -v /etc/nginx/nginx.conf:/etc/nginx/nginx.conf:ro -d nginx
5. 测试
浏览器键入地址测试