nginx 基础配置文件
vim /etc/nginx/nginx.conf
CoreModule 核心模块 (进程数等)
EventsModule 事件驱动模块(工作模式等)
HttpCoreModule http内核模块(文档程序类型,配置文件等)
模块功能
1、全局/核心块:配置影响nginx全局的指令。一般有运行nginx服务器的用户组,nginx进程pid存放路径,日志存放路径,配置文件引入,允许生成worker process数等。
2、events块:配置影响nginx服务器或与用户的网络连接。有每个进程的最大连接数,选取哪种事件驱动模型处理连接请求,是否允许同时接受多个网路连接,开启多个网络连接序列化等。
3、http块:可以嵌套多个server,配置代理,缓存,日志定义等绝大多数功能和第三方模块的配置。如文件引入,mime-type定义,日志自定义,是否使用sendfile传输文件,连接超时时间,单连接请求数等。
4、server块:配置虚拟主机的相关参数,一个http中可以有多个server。
5、location块:配置请求的路由,以及各种页面的处理情况。
user nginx;
运行nginx程序的独立账号
worker_processes 1;
启动的worker进程数量(CPU数量一致或auto)
error_log /var/log/nginx/error.log warn;
错误日志存放位置
pid /var/run/nginx.pid;
events {
事件
use epoll;
事件驱动模型epoll【默认】
事件驱动模型分类,select|poll|kqueue|epoll|resig|/dev/poll|eventport
worker_connections 10240;
//每个worker进程允许处理的最大连接数,例如10240,65535
}
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;
优化参数
高效传输文件的模式
Nginx高级篇sendfile配置
sendfile: 设置为on表示启动高效传输文件的模式。sendfile可以让Nginx在传输文件时直接在磁盘和tcp socket之间传输数据。如果这个参数不开启,会先在用户空间(Nginx进程空间)申请一个buffer,用read函数把数据从磁盘读到cache,再从cache读取到用户空间的buffer,再用write函数把数据从用户空间的buffer写入到内核的buffer,最后到tcp socket。开启这个参数后可以让数据不用经过用户buffer。
\#tcp_nopush on;
优化参数
也就是说tcp_nopush = on 会设置调用tcp_cork方法,这个也是默认的,结果就是数据包不会马上传送出去,等到数据包最大时,一次性的传输出去,这样有助于解决网络堵塞。
keepalive_timeout 65;
优化参数
长连接
\#gzip on;
压缩参数
include /etc/nginx/conf.d/*.conf;
包含子配置文件夹
vim /etc/nginx/conf.d/default.conf
server {
默认网站配置文件
listen 80;
监听端口
server_name localhost;
FQDN
#charset 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;
#}
}