首先我们来确定下今天实验的目标:
- 完成nginx 在容器中的创建
- 完成nginx 实现反向代理
- 完成nginx与域名之间的绑定
- 完成nginx 和不同域名之间的绑定,完成不同系统入口的指定
1 nginx
1.1 nginx安装
docker run -di --name=liubijun_nginx -p 80:80 nginx
docker start liubijun_nginx
完成容器的启动,发现nginx 网站已经能够正常访问了。
1.2 修改配置文件的方法
#将容器中的配置文件拷贝到本地进行修改
docker cp liubijun_nginx:/etc/nginx/nginx.conf nginx.conf
docker cp liubijun_nginx:/etc/nginx/conf.d/default.conf default.conf
#将修改好的配置文件还是覆盖之前的容器中的配置文件
docker cp default.conf liubijun_nginx:/etc/nginx/conf.d/default.conf
docker cp /root/nginx.conf liubijun_nginx:/etc/nginx/nginx.conf
#覆盖好配置文件后,记得重新启动容器
docker restart liubijun_nginx
1.3 配置解释
nginx.conf文件
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
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;
#这里设置的是方向代理的服务的名称
upstream server111{
server 192.168.10.107:2222 weight=1;
#这个地方配置的是反向代理的服务器地址和端口号,后 面的weight 是表示权重。权重越高负载均衡的的命中率越高
server 192.168.10.107:3333 weight=1;
}
include /etc/nginx/conf.d/*.conf; #通过这句话我们知道,nginx的配置文件
}
default.conf 文件
server {
listen 80;
server_name test.domain.com; #这里是绑定1号域名的地方
location = / {
proxy_pass http://server111/page/login; #当访问地址为根的时候,我们指向我们需要的系统的入口,这样可以屏蔽端口号等优点
}
location / {
proxy_pass http://server111/; #当让问的地址是带路劲的时候,我们启用这条规约,让nginx为我们转发代理这个服务。
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
server {
listen 80;
server_name test.sx.com; #这是绑定2号域名的这方
location = / {
proxy_pass http://server111/page/index; #当域名为2号的时候,我们通过这个入口来进入我们的系统
}
location / {
proxy_pass http://server111/; #当域名带路劲访问的时候,我们来代理我们的应用程序
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
1.4 修改hosts文件
建议大家可以使用工具,要是没有也可以给我留言。
1.5 实验结果
1.6 docker容器工具两个很重要的命令
#查看当前容器的状态。因为你修改nginx 配置文件后,可能会出现问题,所以你要查看下容器有没有启动
docker ps
#查看容器的日志命令
#格式: docker logs -f -t -- tail 记录数 容器名称
docker logs -f -t --tail 100 liubijun_nginx