docker-nginx实践

文章目录

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

  • 以下是实际项目的目录及配置,仅供参考
    docker-nginx实践
    其中start.sh内容如下

    #!/bin/sh
    
    docker run -it -d \
    -p 8401:8401:wq \
    -v $PWD/html:/usr/share/nginx/html \
    -v $PWD/conf/nginx.conf:/etc/nginx/nginx.conf \
    -v $PWD/myconf/:/etc/nginx/conf.d \
    --link rentapp \
    --link rentweb \
    --name nginx \
    --restart always \
    nginx:1.15.8
    
  • 其中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;
            }
    
    }