Nginx的配置文件组成部分:
- 主配置文件:nginx.conf
- 子配置文件:include conf.d/*.conf
注意:
- 指令必须以分号结尾
- 支持使用配置变量
内建变量:由nginx模块引入,可直接引用
自定义变量:由用户使用set命令定义 set variable_name value
引用变量:$variable_name
下面就来讲解nginx配置文件。
nginx核心配置详解
1.全局配置
user nginx nginx; #启动Nginx工作进程的用户和组
worker_processes [number | auto]; #启动Nginx工作进程的数量
worker_cpu_affinity 00000001 00000010 00000100 00001000; #将Nginx工作进程绑定到指定的 CPU核心,默认Nginx是不进行进程绑定的,绑定并不是意味着当前nginx进程独占以一核心CPU,但是可以保证 此进程不会运行在其他核心上,这就极大减少了nginx的工作进程在不同的cpu核心上的来回跳转,减少了CPU 对进程的资源分配与回收以及内存管理等,因此可以有效的提升nginx服务器的性能。
#错误日志记录配置,语法:error_log file [debug | info | notice | warn | error | crit | alert | emerg]
error_log logs/error.log;
error_log logs/error.log notice;
error_log /apps/nginx/logs/error.log error;
#pid文件保存路径
pid /apps/nginx/logs/nginx.pid;
worker_priority 0; #工作进程优先级,-20~19
master_process off|on; #是否开启Nginx的master-woker工作模式。
events {
worker_connections 66536;#设置单个nginx工作进程可以接受的最大并发,作为web服务器的 时候最大并发数为worker_connections * worker_processes,作为反向代理的时候为 (worker_connections * worker_processes)/2
use epoll; #使用epoll事件驱动模型,nginx支持众多模型,如:select,poll,epoll,只能设置在events模块中
accept_mutex on; #优化同一时刻只有一个请求而避免多个睡眠进程被唤醒的设置
multi_accept on; #nginx服务器的每个工作进程可以同时接收多个网络请求,但是需要在配置文件中配置,此指令默认为关闭,
}
2.http模块详细配置
http {
include 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 logs/access.log main;
#自定义优化参数
sendfile on; #指定是否使用sendfile系统调用来传输文件,sendfile系统调用在两个文件描述符之间直接传递数据
#tcp_nopush on; #在开启了sendfile的情况下,合并请求后同一发送给客户端
#tcp_nodelay off; #在开启了keepalived模式下的连接是否启动tcp_nodelay选项,当为off时,延迟发送,合并多个请求后在发送,默认on时,不延迟发送,立即发送用户相应报文
keepalive_timeout 65; #设置会话保持时间
#gzip on; #开启文件压缩
server {
listen 80; #设置监听端口
server_name localhost; #设置servername,可以以空格隔开写多个
#charset koi8-r; #设置编码格式,可以修改为utf-8
#access_log logs/host.access.log main;
location / {
root /data/nginx/wordpress; #前端页面路径
index index.php index.html index.htm; #指定默认的网页文件,
}
location ~* \.(jpg|png)$ {
root /data/nginx/static/images;
index index.php 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 html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ { #以http的方式转发php请求到指定web服务器
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ { #以fastcgi方式转发php请求到php处理
root /data/nginx/wordpress;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
#https服务器配置
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}