拉取基于 alpine 的 nginx 官方镜像
docker pull nginx:1.13.8-alpine
容器生成后进入:
docker exec -it 容器ID /bin/ash
网站文件存放默认目录:
/usr/share/nginx/html
网站默认站点配置:
/etc/nginx/conf.d/default.conf
自定义Nginx站点配置文件存放目录:
/etc/nginx/conf.d/
Nginx全局配置:
/etc/nginx/nginx.conf
在全局配置里可以看到:
include /etc/nginx/conf.d/*.conf;
- 一个配置文件中多个配置可以用:
server{
...
}
括起来
docker nginx 全局配置文件解释(
http://www.nginx.cn/76.html):
user nginx;
#运行用户
worker_processes 1;
#启动进程,通常设置成和cpu的数量相等
error_log /var/log/nginx/error.log warn;
#全局错误日志
pid /var/run/nginx.pid;
#进程代号
#工作模式及连接数上限
events {
worker_connections 1024;
#单个后台worker process进程的最大并发链接数
}
http {
#该文件定义mime
类型(Multipurpose Internet Mail Extensions代表互联网媒体类型(Internet media type),MIME使用一个简单的字符串组成,最初是为了标识邮件Email附件的类型,在html文件中可以使用content-type属性表示,描述了文件类型的互联网标准。MIME类型能包含视频、图像、文本、音频、应用程序等数据。)
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 指令指定 nginx 是否调用 sendfile 函数(zero copy 方式)来输出文件,
#对于普通应用,必须设为 on,
#如果用来进行下载等应用磁盘IO重负载应用,可设置为 off,
#以平衡磁盘与网络I/O处理速度,降低系统的uptime.
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#连接超时时间
#gzip on;
#开启gzip压缩
include /etc/nginx/conf.d/*.conf;
#引用该文件夹下的配置
}
/etc/nginx/conf.d/*.conf 下的配置文件,主要用来配置虚拟主机:
server {
listen 80; #监听的端口
server_name localhost; #定义使用localhost访问
#charset koi8-r; #字符集设置模块(koi8-r是一种俄语编码)
#access_log /var/log/nginx/host.access.log main;
#默认请求
location / {
root /usr/share/nginx/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;
#}
}
- 遇到这种情况:
2018/02/06 09:45:18 [error] 9#9: *2 open() "/usr/share/nginx/html/favicon.ico" failed (2: No such file or directory), client: 172.17.0.1, server: localhost, request: "GET /favicon.ico HTTP/1.1", host: "localhost:32800", referrer: "
http://localhost:32800/
"
在 server 中添加:
location = /favicon.ico { ## 采用完全匹配模式
log_not_found off; ## 不写 error.log
access_log off; ## 不写 access.log
}