docker单机部署Nginx
部署环境
CentOS 7
部署工具
docker
Nginx
部署步骤
docker拉取镜像
在shell中运行以下命令拉取官方nginx容器镜像
docker pull docker.io/nginx
设置nginx配置文件
如对应本地目录下没有 nginx.conf 和 default.conf 配置文件将会启动报错,因此需要预先从已运行的nginx中将文件复制出来置于本地目录下,例如/myDockerData/Nginx/nginx.conf 和 /myDockerData/Nginx/default.conf。为方便复制,列出文件内容如下,将其复制到本地映射路径下 nginx.conf 和 default.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;
}
- default.conf
Nginx的默认网站根目录为/usr/share/nginx/html, 此处已修改为/www/html。
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
location / {
root /www/html;
index index.html index.htm;
}
#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/share/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 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;
#}
部署静态网页文件
Nginx做负载平衡服务器时也可以作为静态网页文件服务器,访问动态网页时才访问tomcat,做到动静分离。
将静态网页置入本地路径的Nginx网站根目录/myDockerData/Nginx/www/html,如图所示:

运行容器
在shell中运行以下命令运行容器,启动nginx。
docker run --name nginx-81 -it --restart=always \
-p 81:80 \
-v /etc/localtime:/etc/localtime \
-v /myDockerData/Nginx/logs:/var/log/nginx:z \
-v /myDockerData/Nginx/nginx.conf:/etc/nginx/nginx.conf:z \
-v /myDockerData/Nginx/default.conf:/etc/nginx/conf.d/default.conf:z \
-v /myDockerData/Nginx/www/html:/www/html:z \
-d docker.io/nginx
容器运行参数说明
- -p:端口映射,格式 [本地端口]:[容器端口]。如需运行多个Nginx容器做高可用,可以修改本地端口来运行多个nginx服务,如
81:80,82:80等。 - -v:目录及文件映射。格式 [本地文件]:[容器文件]:[权限],会将本地文件或目录覆盖到容器对应目录。
-v /etc/localtime:/etc/localtime:设置容器时间与本地服务器一致。-v /myDockerData/Nginx/logs:/var/log/nginx:z:将Nginx的日志目录映射出来方便查看。为通过SELinux的权限控制设置权限为:z。-v /myDockerData/Nginx/nginx.conf:/etc/nginx/nginx.conf:z:将nginx配置文件nginx.conf映射到本地文件方便修改,为通过SELinux的权限控制设置权限为:z。-v /myDockerData/Nginx/default.conf:/etc/nginx/conf.d/default.conf:z:将nginx配置文件default.conf映射到本地文件方便修改,为通过SELinux的权限控制设置权限为:z。-v /myDockerData/Nginx/www/html:/www/html:z:将nginx网站根目录映射到本地目录方便修改,为通过SELinux的权限控制设置权限为:z
部署完成
在浏览器打开Nginx网站,地址为IP:port,例如 localhost:81。
如可成功显示静态网页则部署成功。
猫肚肚是乌贼和人类心灵永远的家园。 —— 原创文章,转载请注明出处

浙公网安备 33010602011771号