docker下nginx反向代理和负载均衡配置
拉取镜像
docker pull nginx docker pull tomcat
启动docker
docker run --name=my_nginx -p 8000:80 -d nginx --name: Ϊnginx容器指定一个名称方便管理 -p: 将nginx内部80端口代理到宿主机8000端口,可以通过宿主机:8000访问nginx 80端口 -d: 后台运行
查看容器运行情况
docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 3694b7d63cab nginx "nginx -g 'daemon of…" 2 minutes ago Up 2 minutes 0.0.0.0:8000->80/tcp my_nginx
访问nginx
http://node3.liudehan.com:8000/
进入容器后台
docker exec -it my_nginx bash root@3694b7d63cab:/etc/nginx# ls -l total 40 drwxr-xr-x 2 root root 4096 May 8 03:01 conf.d -rw-r--r-- 1 root root 1007 Apr 16 13:08 fastcgi_params -rw-r--r-- 1 root root 2837 Apr 16 13:08 koi-utf -rw-r--r-- 1 root root 2223 Apr 16 13:08 koi-win -rw-r--r-- 1 root root 5231 Apr 16 13:08 mime.types lrwxrwxrwx 1 root root 22 Apr 16 13:08 modules -> /usr/lib/nginx/modules -rw-r--r-- 1 root root 643 Apr 16 13:08 nginx.conf -rw-r--r-- 1 root root 636 Apr 16 13:08 scgi_params -rw-r--r-- 1 root root 664 Apr 16 13:08 uwsgi_params -rw-r--r-- 1 root root 3610 Apr 16 13:08 win-utf
nginx.conf是nginx主要配置文件,可以通过more命令查看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; include /etc/nginx/conf.d/*.conf; }
include /etc/nginx/conf.d/*.conf;
导入配置文件如下
root@3694b7d63cab:/etc/nginx/conf.d# cat default.conf server { listen 80; server_name localhost; #charset koi8-r; #access_log /var/log/nginx/host.access.log main; location / { root /usr/share/nginx/html; index index.html index.htm; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ \.php$ { # proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # #location ~ \.php$ { # root html; # fastcgi_pass 127.0.0.1:9000; # fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; # include fastcgi_params; #} # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #} }
nginx配置文件挂载
创建文件和目录
mkdir -p u01/nginx mkdir -p u01/nginx/conf.d touch u01/nginx/nginx.conf touch u01/nginx/conf.d/default.conf
删除镜像,重新挂载
docker stop my_nginx docker rm my_nginx docker run --name=my_nginx -v /Users/liudehan/u01/nginx/nginx.conf:/etc/nginx/nginx.conf -v /Users/liudehan/u01/nginx/conf.d:/etc/nginx/conf.d -p 80:80 -d nginx
更改配置加载web应用
docker stop my_nginx docker rm my_nginx docker run --name=my_nginx -v /Users/liudehan/u01/nginx/nginx.conf:/etc/nginx/nginx.conf -v /Users/liudehan/u01/nginx/conf.d:/etc/nginx/conf.d -v /Users/liudehan/u01/nginx/www/dist-H5:/usr/share/nginx/html/dist-H5 -p 80:80 -d nginx
创建Tomcat应用
应用1 docker run --name=my_tomcat1 -v/Users/liudehan/u01/webapps:/usr/local/tomcat/webapps -p 8001:8080 -d tomcat 应用2 docker run --name=my_tomcat2 -v/Users/liudehan/u01/webapps:/usr/local/tomcat/webapps -p 8002:8080 -d tomcat
显示如下: my_tomcat1
my_tomcat2nginx实现反向代理和负载均衡
nginx配置如下
default.conf
location / { proxy_pass http://tomcat_server;
nginx.conf配置如下
http里面 upstream tomcat { //这不能为tomcat_server这样的,这是域名 server t1:8080; }
配置docker如下
docker run --name=my_nginx --link=my_tomcat1:t1 --link=my_tomcat2:t2 -v /Users/liudehan/u01/nginx/nginx.conf:/etc/nginx/nginx.conf -v /Users/liudehan/u01/nginx/conf.d:/etc/nginx/conf.d -p 80:80 -d nginx
显示如下: