基于Nginx Docker容器的系统部署

  系统采用前后端分别部署方式, 后端采用SpringBoot框架, 部署在单独的容器内,对外放8080端口。前端通过Nginx的反向代理访问后端8080端口。

后端容器部署

   如果前后端容器部署在同一台物理机, 为保障系统安全,可以把后端、数据的端口不映射到主机端口,创建名为sshl的容器(镜像名为seed-server)时,如下:

docker run --restart=always --name sshl -d seed-server
查看容器地址:
docker inspect sshl
 
 "Networks": {
             "bridge": {
                 "IPAMConfig": null,
                 "Links": null,
                 "Aliases": null,
                 "NetworkID": "f0c831e7265d9edc3a0541aa1a7483448b067757687d28e229284c32a54b7dd9",
                 "EndpointID": "a210475489c8f7d1b481a0fce3e4f0c8e4caf64e9ffa3461d856eb9cf9f47999",
                 "Gateway": "172.17.0.1",
                 "IPAddress": "172.17.0.4",
                 "IPPrefixLen": 16,
                 "IPv6Gateway": "",
                 "GlobalIPv6Address": "",
                 "GlobalIPv6PrefixLen": 0,
                 "MacAddress": "02:42:ac:11:00:04",
                 "DriverOpts": null
             }
   

可以看到容器地址是172.17.0.4

前端Nginx 容器部署

安装Nginx 容器

(略)

修改nginx.conf



   docker run -d   nginx
   # b72e67077708  为新建的容器ID
   docker cp b72e67077708:/etc/nginx/nginx.conf /mnt/data/nginx/conf/
   docker cp b72e67077708:/usr/share/nginx/html/index.html /mnt/data/nginx/www/
   docker stop b72e67077708
   docker rm b72e67077708
 

修改:/mnt/data/nginx/conf/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;
       # 下面两个参数设置为on用于防止网络阻塞
       tcp_nopush     on;
       tcp_nodelay    on;
       #客户端连接保持活动的超时时间
       keepalive_timeout  65;

       gzip  on;

       include /etc/nginx/conf.d/*.conf;
       server {
           listen        80;
		   # server_name  为宿主机地址
           server_name  192.168.204.181;
		   
		   
        root   /usr/share/nginx/html;
       
           #charset koi8-r;
           #access_log  /var/log/nginx/host.access.log  main;

           location / {
               index  index.html index.htm;
           }
			
		#  系统里添加反向代理/api  ,172.17.0.4:8080 是后端地址	
		    location /api { 
            rewrite ^/api/(.*)$ /$1 break;
            proxy_pass http://172.17.0.4:8080;
        }	
           error_page  404              /404.html;
        location = /40x.html {
        }

           # redirect server error pages to the static page /50x.html
           error_page   500 502 503 504  /50x.html;
           location = /50x.html {
           }
       }
   }



重建Nginx 容器



docker run -p 80:80  --restart=always  --name nginx \
     -v /mnt/data/nginx/www:/usr/share/nginx/html \
     -v /mnt/data/nginx/conf/nginx.conf:/etc/nginx/nginx.conf \
     -v /mnt/data/nginx/logs:/var/log/nginx \
     -d nginx


  • 访问系统:http://192.168.204.181 即可
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值