使用docker搭建nginx服务
1. 拉取nginx镜像
docker pull nginx
2. 简单启动nginx服务
docker run -d -p 80:80 --name nginx-server nginx
参数说明:
run 运行容器
-d = daemon 后台运行,守护进程
-p 端口映射。顺序为: 宿主机端口(即外部端口):容器端口(容器内部端口)
--name. 给容器起一个名字,方便操作的时候使用容器名称,也可以使用容器ID
nginx. 最后使用指定的基础镜像
3. 容器内目录结构简单介绍
# 进入容器内部,查看目录
docker exec -it nginx-server /bin/bash
参数说明:
exec。进入容器命令
-it 使用交互式进入容器
nginx-server 指定进入的容器
/bin/bash 使用bash方式进行操作
配置文件:/etc/nginx/
日志文件:/var/log/nginx/
4. 目录映射
将容器中的nginx配置目录与映射到宿主机上: 将 /etc/nginx ==> /home/data
docker cp nginx-server:/etc/nginx /home/data
参数说明:
cp 拷贝命令 容器:文件位置。 宿主机文件位置
建立一个目录的映射关系
5. nginx.conf 配置文件简要说明
配置文件: nginx.conf
1
2 user nginx; # nginx用户
3 worker_processes 1; # 启动的worker进程的数量,一般与核心数相同即可
4
5 error_log /var/log/nginx/error.log warn; # 定义错误日志文件目录与错误级别
6 pid /var/run/nginx.pid; # 进程
7
8
9 events {
10 worker_connections 1024; # 每个worker进程支持的最大链接数, 总的nginx支持的连接总数=worker_processes*worker_connections
11 }
12
13
14 http { # http服务配置
15 include /etc/nginx/mime.types; # 支持的媒体类型
16 default_type application/octet-stream;
17
# 定义日志格式
18 log_format main '$remote_addr - $remote_user [$time_local] "$request" '
19 '$status $body_bytes_sent "$http_referer" '
20 '"$http_user_agent" "$http_x_forwarded_for"';
21
22 access_log /var/log/nginx/access.log main; # 访问日志
23
24 sendfile on; # 加快访问, 建议开启
25 #tcp_nopush on;
26
27 keepalive_timeout 65; # 设置超时时间
28
29 #gzip on; # gzip 压缩
30
31 include /etc/nginx/conf.d/*.conf;
32 }
1 server { # 配置的虚拟主机
2 listen 80; # 监听端口
3 server_name localhost; # 指定域名或IP地址访问也行
4
5 #charset koi8-r;
6 #access_log /var/log/nginx/host.access.log main; # 设置该域名的访问日志
7
8 location / {
9 root /usr/share/nginx/html; # 访问路径
10 index index.html index.htm; # 访问文件
11 }
12
13 #error_page 404 /404.html;
14
15 # redirect server error pages to the static page /50x.html
16 #
17 error_page 500 502 503 504 /50x.html;
18 location = /50x.html { # 找不到文件的时候,发生500事件
19 root /usr/share/nginx/html;
20 }
21
22 # proxy the PHP scripts to Apache listening on 127.0.0.1:80
23 #
24 #location ~ \.php$ { # 设置反向代理
25 # proxy_pass http://127.0.0.1;
26 #}
27
28 # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
29 #
30 #location ~ \.php$ { # PHP文件
31 # root html;
32 # fastcgi_pass 127.0.0.1:9000; # 转发给php-fpm处理
33 # fastcgi_index index.php;
34 # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
35 # include fastcgi_params; # 包含的变量
36 #}
37
38 # deny access to .htaccess files, if Apache's document root
39 # concurs with nginx's one
40 #
41 #location ~ /\.ht {
42 # deny all;
43 #}
44 }