Nginx系列(九)——容器/微服务

Containers/Microservices
容器/微服务
Using the Official NGINX Image
docker run --name my-nginx -p 80:80 -v /path/to/content:/usr/share/nginx/html:ro -d nginx #-v本地目录映射到容器目录,使用只读模式ro。其他的也是一般是本地的在前,容器的在后


Creating an NGINX Dockerfile
Dockerfile:
FROM centos:7
# Install epel repo to get nginx and install nginx
RUN yum -y install epel-release && \
yum -y install nginx
# add local configuration files into the image
ADD /nginx-conf /etc/nginx #配置nginx配置文件
EXPOSE 80 443 #暴露端口
CMD ["nginx"] #启动nginx

The directory structure looks as follows:
.
├── Dockerfile
└── nginx-conf
├── conf.d
│ └── default.conf
├── fastcgi.conf
├── fastcgi_params
├── koi-utf
├── koi-win
├── mime.types
├── nginx.conf
├── scgi_params
├── uwsgi_params
└── win-utf


Using Environment Variables in NGINX
配置如下
daemon off;
env APP_DNS;
include /usr/share/nginx/modules/*.conf;
...
http {
perl_set $upstream_app 'sub { return $ENV{"APP_DNS"}; }'; #perl_set需要提前安装ngx_http_perl_module
server {
...
location / {
proxy_pass https://$upstream_app;
}
}
}
相关的dockerfile
FROM centos:7
# Install epel repo to get nginx and install nginx
RUN yum -y install epel-release && \
yum -y install nginx nginx-mod-http-perl
# add local configuration files into the image
ADD /nginx-conf /etc/nginx
EXPOSE 80 443
CMD ["nginx"]