docker-nginx实践

5 篇文章 1 订阅
2 篇文章 0 订阅

文章目录

docker-nginx实践

  • nginx dockerhub官方地址:https://hub.docker.com/_/nginx
  • 官方镜像里已经有nginx的使用方法
  • 需要注意的是官网这段说明,这样操作以后能把nginx.conf从容器内拷贝出来
Complex configuration

$ docker run --name my-custom-nginx-container -v /host/path/nginx.conf:/etc/nginx/nginx.conf:ro -d nginx
For information on the syntax of the nginx configuration files, see the official documentation (specifically the Beginner's Guide).

If you wish to adapt the default configuration, use something like the following to copy it from a running nginx container:

$ docker run --name tmp-nginx-container -d nginx
$ docker cp tmp-nginx-container:/etc/nginx/nginx.conf /host/path/nginx.conf
$ docker rm -f tmp-nginx-container
This can also be accomplished more cleanly using a simple Dockerfile (in /host/path/):

FROM nginx
COPY nginx.conf /etc/nginx/nginx.conf
If you add a custom CMD in the Dockerfile, be sure to include -g daemon off; in the CMD in order for nginx to stay in the foreground, so that Docker can track the process properly (otherwise your container will stop immediately after starting)!

Then build the image with docker build -t custom-nginx . and run it as follows:

$ docker run --name my-custom-nginx-container -d custom-nginx
  • 上面的意思就是如果你想修改默认配置,那么最方便的就是把容器内的/etc/nginx/nginx.conf文件拷出来,映射到宿主机,这样修改默认配置就不用进入容器内操作这么麻烦了

  • 以下是实际项目的目录及配置,仅供参考
    在这里插入图片描述
    其中start.sh内容如下

    #!/bin/sh
    
    docker run -it -d \
    --net=host \
    -v /etc/localtime:/etc/localtime:ro \
    -v $PWD/html:/usr/share/nginx/html \
    -v $PWD/conf/nginx.conf:/etc/nginx/nginx.conf \
    -v $PWD/myconf/:/etc/nginx/conf.d \
    --link rentweb \
    --link rentapp \
    --name nginx \
    --restart always \
    nginx:1.15.8
    
  • 另外提供docker-compose.yml

    version: '3'
    services:
      nginx:
        container_name: vds-nginx
        network_mode: host
        restart: always
        image: nginx:1.15.8
        volumes:
          - /etc/localtime:/etc/localtime:ro
          - $PWD/html:/usr/share/nginx/html
          - $PWD/myconf/:/etc/nginx/conf.d
          - $PWD/conf/nginx.conf:/etc/nginx/nginx.conf
    
  • 其中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;
    
    }
    
  • 其中myconf/nginx-rent.conf内容如下:

    server{
    
            listen 8401;
            server_name localhost;
            client_max_body_size 512m;
    
            location / {
            root /usr/share/nginx/html/rent/web;
            index index.html index.htm;
            try_files $uri $uri/ /index.html;
            add_header 'Access-Control-Allow-Origin' '*';
            }
    
    
            location /view/ {
            root /usr/share/nginx/html/rent;
            index index.html index.htm;
            add_header 'Access-Control-Allow-Origin' '*';
            }
    
    
            location /api/ {
            proxy_pass http://rentweb:8201; #web后台接口地址
            }
    
    
            location /v1/ {
            proxy_pass http://rentapp:8101; #app后台接口地址
            }
    
            error_page 500 502 503 504 /50x.html;
            location = /50x.html {
            root html;
            }
    
    }
    
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值