通过docker安装的nginx配置文件如下 将逐一讲解及验证
user nginx;
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;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
}
user:标识使用nginx的用户名
worker_processes(工作线程):nginx是通过一个主进程运行但处理网络请求是工作线程通过该参数设定工作线程的数目 auto为自动根据CPU核数设置
error_log:指定nginx的日志文件存储路径及其日志级别
pid:存储nginx主进程号的文件位置
events:指令模块 可以理解为指令集合里面配置多条指令
worker_connections:工作线程能处理的最大连接数(如果并发比较高可以修改大一些,操作系统默认也是1024,改大的同时也需要修改一下系统设置 ulimit -n)
http:指令模块 常配置http服务的指令
include: 引入其他配置文件。这里引用了 mime.types配置文件里面定义了能处理的文件类型如html,xml,js等。
default_type:如果include中的配置都匹配不上则默认按这里定义的文件类型处理(二进制文件流)
log_format:日志的打印输出格式。
access_log:nginx处理网络请求的日志文件存储路径 后面的main则是关联使用main定义的log_format格式记录日志
sendfile:发送文件时的0拷贝设置 通常会配合下面的tcp_nopush一起使用以减少网络消耗
keepalive_timeout:TCP长连接的设置的超时时间
gzip:响应体压缩,以节约网络开销
最后的include引入server指令模块的web服务配置
一般来说一台主机部署一个web服务,想要部署多个web服务相对应的就是多台主机,但这样有点浪费。于是便有了虚拟主机的概念。一台nginx是上部署多个web网站的 其中生产中就通过include /etc/nginx/conf.d/*.conf;引入不同web服务的配置文件将不同的web服务配置互相隔离
引入的service模块配置如下
server {
listen 80;
listen [::]:80;
server_name localhost;
#access_log /var/log/nginx/host.access.log main;
#设置字符集
charset utf-8;
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;
#}
}
listen:监听的端口
charset:字符集编码
location:访问web服务资源的路径 root是定义根路径,index是定义默认访问的资源路径
浙公网安备 33010602011771号