2016年10月16日 星期日

Dockerfile nodejs + nginx

因為覺得 Docker 應該可以獨領風燒 下個 5 至 10年,就學了

Docker file

FROM node:6.8.0-onbuild
#從onbuild那裡拿,丟aws eb 比較不會有問題

ENV NGINX_VERSION 1.11.5-1~jessie
#nginx的veriosn

RUN apt-key adv --keyserver hkp://pgp.mit.edu:80 --recv-keys 573BFD6B3D8FBC641079A6ABABF5BD827BD9BF62 \
&& echo "deb http://nginx.org/packages/mainline/debian/ jessie nginx" >> /etc/apt/sources.list \
&& apt-get update \
&& apt-get install --no-install-recommends --no-install-suggests -y \
ca-certificates \
nginx=${NGINX_VERSION} \
nginx-module-xslt \
nginx-module-geoip \
nginx-module-image-filter \
nginx-module-perl \
nginx-module-njs \
gettext-base \
&& rm -rf /var/lib/apt/lists/*
#下載 nginx

# forward request and error logs to docker log collector
RUN ln -sf /dev/stdout /var/log/nginx/access.log \
&& ln -sf /dev/stderr /var/log/nginx/error.log
#做 link

# Remove the default Nginx configuration file
RUN rm -v /etc/nginx/nginx.conf
#移除nginx原本的conf設定檔

# Copy a configuration file from the current directory
ADD nginx.conf /etc/nginx/
#新增本機端的conf設定檔到image裡


# Append "daemon off;" to the configuration file
RUN echo "daemon off;" >> /etc/nginx/nginx.conf
#用docker 的話,nginx設定檔得加一行

ADD default.conf /etc/nginx/sites-enabled
ONBUILD COPY . /usr/src/app
#將目前工作目錄copy 過來

CMD service nginx start & npm start;
#一定要&加在一起,而且只能一行,不然後行會取代前行...

EXPOSE 80 3000-3002
#因為新的nginx.conf設定從3000port轉過來,並且有設定gzip


nginx.conf
//proxy_pass          http://localhost:3000; 直接轉3000 port的資料來用
//gzip_types  開啟那些附檔名

worker_processes 4;

events { worker_connections 1024; }

http {

   
        server {
            listen 80;
            server_name localhost

            client_max_body_size 1000M;

            access_log            /var/log/nginx/http.log;

            gzip on;
            gzip_disable "msie6";

            gzip_vary on;
            gzip_proxied any;
            gzip_comp_level 6;
            gzip_buffers 16 8k;
            gzip_http_version 1.1;
            gzip_min_length 256;
            gzip_types text/html text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript application/vnd.ms-fontobject application/x-font-ttf font/opentype image/svg+xml image/x-icon;

            location / {
              proxy_set_header        Host $host;
              proxy_set_header        X-Real-IP $remote_addr;
              proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
              proxy_set_header        X-Forwarded-Proto $scheme;

              proxy_pass          http://localhost:3000;
              proxy_read_timeout  300;
            }
        }

}