Nginx 虚拟主机
我们使用 Docker 来安装和运行 Nginx,nginx的80端口映射到服务器的80端口。可修改。docker-compose.yml 配置如下:
root@faramita:/usr/local/docker/nginx# cat docker-compose.yml
version: '3.1'
services:
nginx:
restart: always
image: nginx
container_name: nginx
ports:
- 80:80
volumes:
- ./conf/nginx.conf:/etc/nginx/nginx.conf
- ./wwwroot:/usr/share/nginx/wwwroot
启动 Tomcat 容器
version: '3'
services:
tomcat1:
image: tomcat
container_name: tomcat1
ports:
- 8080:8080
tomcat2:
image: tomcat
container_name: tomcat2
ports:
- 9090:8080
Nginx 反向代理与负载均衡
配置nginx.conf
这里是通过访问139.224.117.172Nginx的80端口,映射到服务器的80端口,监听80端口,访问到代理服务器myapp1,myapp1有两个tomcat(相当于两台服务器),139.224.117.172:80可随机访问这两个tomcat,实现负载均衡
root@faramita:/usr/local/docker/nginx/conf# cat nginx.conf
user nginx;
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
upstream myapp1 {
server 139.224.117.172:8080 weight=10;
server 139.224.117.172:9090 weight=10;
}
server {
listen 80;
server_name 139.224.117.172;
location / {
proxy_pass http://myapp1;
index index.jsp index.html index.htm;
}
}
}
cdn服务器
docker-compose
root@faramita:/usr/local/docker/nginx# cat docker-compose.yml
version: '3.1'
services:
nginx:
restart: always
image: nginx
container_name: nginx
ports:
- 80:80
volumes:
- ./conf/nginx.conf:/etc/nginx/nginx.conf
- ./wwwroot:/usr/share/nginx/wwwroot
放置cdn中要存放的资源
/usr/local/docker/nginx/wwwroot/cdn/adminlte/v2.4.3
nginx.conf
root@faramita:/usr/local/docker/nginx/conf# cat nginx.conf
user nginx;
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name 139.224.117.172;
location / {
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Headers X-Requested-With;
add_header Access-Control-Allow-Methods GET,POST,OPTIONS;
root /usr/share/nginx/wwwroot/cdn;
index index.jsp index.html index.htm;
}
}
}