Docker下如何优雅的部署Nginx服务器

作者个人博客:http://blog.142vip.cn 点击访问

拉取镜像
$ docker pull nginx
创建映射文件

比如,我希望在/etc/web-project目录下实现对nginx的日志、配置、html网页进行文件映射,需要创建对应的本地映射文件;

  • 创建映射主目录
## 关于nginx容器映射文件,都保存在/etc/web-project/nginx下
$ mkdir nginx
  • 创建对应子文件目录

/etc/web-project/nignx/conf : 挂载容器里面的配置,即nginx.conf

/etc/web-project/nignx/conf.d:挂载容器里面的子配置,即nginx.conf里面include的配置文件

/etc/web-project/nignx/logs :挂载容器里面的代理的日志文件

/etc/web-project/nignx/html :挂载容器里面的界面的访问

## 批量创建
$ mkdir -p /data/nginx/{conf,conf.d,html,logs}
## 注意:批量创建可能会失败,建议手动直接创建; 
创建配置文件
  • 路径:/etc/web-project/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;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
}
  • 路径:/etc/web-project/conf.d/default.conf
server {  
    listen       80;  
    server_name  localhost;  
  
    #charset koi8-r;  
    #access_log  /var/log/nginx/log/host.access.log  main;  
  
    location / {  
        #root   /data/nginx/html;  
        root   /usr/share/nginx/html;  
        index  index.html index.htm;  
        #autoindex  on;  
        #try_files $uri /index/index/page.html;  
        #try_files $uri /index/map/page.html;  
    }  
  
    #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;  
    #}  
}
配置网页html
  • 路径:/etc/web-project/nginx/html/index
<!-- 简单html内容  -->
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title>系统时间</title>
</head>
<body>
<div id="datetime">
    <script>
        setInterval("document.getElementById('datetime').innerHTML=new Date().toLocaleString();", 1000);
    </script>
</div>
</body>  

容器启动

docker run --name nginx -d -p 80:80  \
-v /data/nginx/html:/usr/share/nginx/html  \
-v /data/nginx/conf/nginx.conf:/etc/nginx/nginx.conf  \ 
-v /data/nginx/logs:/var/log/nginx \
-v /data/nginx/conf.d:/etc/nginx/conf.d  nginx:latest

### 可以配置网桥,指定ip
--network=network-bridge --ip 172.0.0.1
容器操作
## 查看正在运行的容器
$ docker ps
## 查看所有容器
$ docker ps -a
## 查看所有镜像
$ docker images
日志查看

当按照上述步骤进行配置、网页、日志文件映射后,可以比较直观的在宿主机里面看到容器内部的文件数据,因为映射之后,宿主机和容器内映射的文件是一一对应的;

可以在/etc/web-project/nignx/logs文件中查看nginx的所有access.log和error.log日志记录;

后续Nginx操作

修改对应的nginx.conf、index.html文件来修改容器配置,修改保存后,采用docker restart nginx启动容器

nginx原样转发

需要告诉NGINX将Host头向上游转发给Kong.您可以使用proxy_set_header这样做:

location /api {
   ## 代理地址
   proxy_pass: http://kong;
   ##  host上游转发
   proxy_set_header Host $host;
}

## 或者设置
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

nginx映射虚拟

## kong-net :名称为kong-net的桥接网络network
$ docker network connect  kong-net nginx-master --ip 172.22.0.9

更多博客内容欢迎访问作者个人博客
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值