1.基于镜像库的nginx再修改nginx配置,编写Dockerfile

from hub.c.163.com/library/nginx:latest
MAINTAINER Jinx----
COPY default.conf /etc/nginx/conf.d/default.conf
COPY proxy.conf /etc/nginx/proxy.conf
COPY nginx.conf /etc/nginx/nginx.conf

进入一个指定的挂载目录
需要复制的文件必须在该目录下
proxy.conf是我自己加的内容如下:

proxy_set_header        Host $host;
proxy_set_header        X-Real-IP $remote_addr;

初始镜像下的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;
}

我修改的内容如下:

user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;

worker_rlimit_nofile 1024;
events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/mime.types;
    include       proxy.conf;
    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;


    server_tokens off;
    sendfile        on;
    tcp_nopush     on;
    server_names_hash_bucket_size 256;
    client_header_buffer_size 256k;

    large_client_header_buffers 4 256k;
    client_body_buffer_size 256k;
    client_header_timeout     3m;
    client_body_timeout 3m;
    send_timeout             3m;

    client_max_body_size 50m;
    keepalive_timeout  120;
    fastcgi_intercept_errors on;
    fastcgi_connect_timeout 300;
    fastcgi_send_timeout 300;
    fastcgi_read_timeout 300;

    fastcgi_buffer_size 128k;
    fastcgi_buffers 4 256k;
    fastcgi_busy_buffers_size 256k;
    fastcgi_temp_file_write_size 256k;

    gzip on;
    gzip_min_length  1k;
    gzip_buffers     4 16k;
    gzip_http_version 1.0;
    gzip_comp_level 2;
    gzip_types       text/plain application/x-javascript text/css application/xml;
    gzip_vary on;

    include /etc/nginx/conf.d/*.conf;
}

关于default.conf就是基本的代理内容,具体如下:

server {

    listen       80;
    server_name  app.xx.com;

    location /{
            proxy_pass    http://139.196.15.xx:8080;
    }

    location /gtw {
            proxy_pass    http://139.196.15.xx:8020;
    }

    location /protops-admin {
        proxy_pass http://139.196.15.xx:8010;
    }
   location /vihicle_uploads {
        proxy_pass http://139.196.15.xx:8030;
    }   
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

}

2.cd到该目录下打包镜像:(具体解释参照上一篇)

docker build -t xx-nginx:latest .

3.启动应用,创建服务

docker run -d -p 900:80 镜像ID
Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐