Linux Docker 安装 Nginx
1、 linux下安装Docker
参考文件:https://www.runoob.com/docker/centos-docker-install.html
https://www.cnblogs.com/myzony/p/9071210.html
官网安装教程:https://docs.docker.com/engine/install/centos/
todo:111111
2、拉取Docker镜像
访问 Nginx 镜像库地址: https://hub.docker.com/_/nginx?tab=tags。
可以选择相关版本:docker pull nginx
默认:latest
使用规则:https://hub.docker.com/_/nginx?tab=description&page=2
在宿主机中创建挂载目录
mkdir -p /data/nginx/{web,conf,log}
docker run --rm -p80:80 --name nginx \
-v /data/nginx/web:/usr/share/nginx/html \
-v /data/nginx/conf/nginx.conf:/etc/nginx/nginx.conf \
-v /data/nginx/log:/var/log/nginx \
-d nginx:latest
名词解析:
run:创建并运行一个容器
–rm:停止删除容器
-p 80:80,宿主机80映射到容器内的80
-v :宿主机路径,映射到容器内
–name:指定创建的容器的名字
-d nginx:latest 运行指定的镜像
/data/nginx/web:宿主机路径
/usr/share/nginx/html:nginx 容器内的路径
nginx.conf 文件
user root;
worker_processes auto;
error_log /var/log/nginx/error.log notice;
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;
client_max_body_size 1024m;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/conf.d/*.conf;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_comp_level 7;
gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;
gzip_vary off;
gzip_disable "MSIE [1-6]\.";
# 代理
#upstream backend {
# server 192.168.0.88:8080;
#}
# develop
server {
listen 80 default_server;
server_name _;
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
try_files $uri $uri/ /index.html;
}
#location /backend/ {
# proxy_pass http://backend;
#}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
}
重启nginx容器:docker restart nginx