nginx部署docker化
大约 3 分钟
nginx部署docker化
docker compose 模式
首先创建一个名为Nginx的文件夹,然后在里面创建一个html文件夹以及config.d文件夹(这两个分别是网站目录以及nginx配置目录)
mkdir -p /data0/Nginx
cd Nginx
mkdir html
mkdir config.d
创建Dockerfile和docker-compose.yml
touch Dockerfile
touch docker-compose.yml
docker-compose.yml的内容:
version: '3.3' #这个是docker的版本
services:
blog:
container_name: blog #容器名称
build: . #构建目录
ports: #端口映射 对应 -p
- "80:80"
- "443:443"
volumes: #目录挂载 对应 -v
- /data0/Nginx/log:/usr/local/openresty/nginx/logs
Dockerfile的内容:
FROM openresty/openresty
USER root
EXPOSE 80
EXPOSE 443
RUN rm -rf /etc/nginx/conf.d
RUN rm -rf /usr/local/openresty/nginx/html
COPY ./html /usr/local/openresty/nginx/html
COPY ./config.d /etc/nginx/conf.d
将以上内容复制粘贴到对应文件中。 然后在config.d文件夹中创建一个配置文件
vim config.d/default.conf
文件内容如下:
server {
listen 80;
server_name localhost;
#charset koi8-r;
access_log /usr/local/openresty/nginx/logs/host.access.log;
location / {
root /usr/local/openresty/nginx/html/default;
index index.html index.htm;
try_files $uri $uri/ /index.html; #这里是为了能使vue运行的配置
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/local/openresty/nginx/html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root /usr/local/openresty/nginx/html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
接着在html文件里面创建index.html
mkdir html/default
echo hello > html/default/index.html
最后回到Nginx目录
cd ..
执行运行命令:
docker-compose up -d
若出现:
Creating network "docker_default" with the default driver
Creating blog ... done
即为成功,可访问对应网址进行判断。 如果需要刷新缓存,则运行:
docker-compose build --no-cache
然后再执行运行命令即可。 注意: 重新构建时记得清除之前构建完的镜像和容器。 关闭服务可使用:
docker-compose down
普通模式
首先拉取nginx镜像:
docker pull openresty/openresty
其次创建文件夹:
mkdir -p /opt/config.d
mkdir -p /opt/www
mkdir -p /opt/log
这里的两个文件夹分别是nginx的配置文件夹和网站文件夹 接着启动容器:
docker run -id --name opennginx -p 80:80 openresty/openresty
然后运行以下命令:
docker cp opennginx:/etc/nginx/conf.d/* /opt/config.d
执行完之后会出现一个配置文件,可直接在里面配置或这替换这个文件,默认配置内容如下:
# nginx.vh.default.conf -- docker-openresty
#
# This file is installed to:
# `/etc/nginx/conf.d/default.conf`
#
# It tracks the `server` section of the upstream OpenResty's `nginx.conf`.
#
# This config (and any other configs in `etc/nginx/conf.d/`) is loaded by
# default by the `include` directive in `/usr/local/openresty/nginx/conf/nginx.conf`.
#
# See https://github.com/openresty/docker-openresty/blob/master/README.md#nginx-config-files
#
server {
listen 80;
server_name localhost;
#charset koi8-r;
access_log /usr/local/openresty/nginx/logs/host.access.log;
location / {
root /usr/local/openresty/nginx/html;
index index.html index.htm;
try_files $uri $uri/ /index.html; #这里是为了能使vue运行的配置
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/local/openresty/nginx/html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root /usr/local/openresty/nginx/html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
配置完成之后执行以下命令
docker run -id --name opennginx \
-p 80:80 \
-p 443:433 \
-v /opt/www:/usr/local/openresty/nginx/html \
-v /opt/config.d:/etc/nginx/conf.d \
-v /opt/log:/usr/local/openresty/nginx/logs \
openresty/openresty
打开浏览器访问对应地址出现内容即为成功。 注意: 这个地址/usr/local/openresty/nginx/html是指你在nginx上面配置的root关键字后面的地址,我这里使用的是默认的,日志地址同理。