配置文件结构
-
Nginx的配置文件是一个纯文本文件,它一般位于Nginx安装目录的conf目录下,整个配置文件是以block的形式组织的。每个block一般以一个大括号“{}”来表示,block可以分为几个层次,整个配置文件中main指令位于最高层,在main层下面可以有Events、HTTP等层级,而在HTTP层中又包含有server层,即server block,server block中又可分为location层,并且一个server block中可以包含多个location block
-
Nginx配置文件主要分为4部分:main(全局设置)、server(主机设置)、upstream(负载均衡服务器设置)和 location(URL匹配特定位置的设置)。main部分设置的指令将影响其他所有设置;server部分的指令主要用于指定主机和端口;upstream指令主要用于负载均衡,设置一系列的后端服务器;location部分用于匹配网页位置。这四者之间的关系如下:server继承main,location继承server,upstream既不会继承其他设置也不会被继承
默认配置文件
#user nobody; # Nginx用户及组:用户 组。window下不指定
worker_processes 1; #工作进程:数目。根据硬件调整,通常等于CPU数量或者2倍于CPU
#全局错误日志存放位置
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid; # pid(进程标识符):存放路径。
#工作模式及连接数上限
events {
use epoll; #epoll是多路复用IO(I/O Multiplexing)中的一种方式
worker_connections 1024; #单个后台worker process进程的最大并发链接数 具体解释见补充
}
http {
include mime.types; #设定mime类型,类型由mime.type文件定义
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; 用了log_format指令设置了日志格式之后,需要用access_log指令指定日志文件的存放路径;
sendfile on; #sendfile指令指定 nginx 是否调用sendfile 函数(zero copy 方式)来输出文件,对于普通应用,必须设为on。如果用来进行下载等应用磁盘IO重负载应用,可设置为off,以平衡磁盘与网络IO处理速度,降低系统uptime。
#tcp_nopush on; #此选项允许或禁止使用socke的TCP_CORK的选项,此选项仅在使用sendfile的时候使用
#keepalive_timeout 0; #连接超时时间, 0表示永不超时
keepalive_timeout 65;
#gzip on; #开启gzip压缩
server {
listen 80; #配置监听端口
server_name localhost; #配置web域名
#charset koi8-r; #配置字符
#access_log logs/host.access.log main; #定义使用的日志格式 main格式已经在全局定义
location / {
root html; #定义服务器的默认网站目录
index index.html index.htm; #定义主页
}
#error_page 404 /404.html; #定义404跳转页面
error_page 500 502 503 504 /50x.html; #定义500 502 503 504 跳转页面
location = /50x.html {
root html;
}
#代理把php的解析请求转发到php解释器上
#location ~ .php$ {
# proxy_pass http://127.0.0.1;
#}
#使用FastCGI解析php
#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;
#}
#定义禁止访问 .ht**文件
#location ~ /.ht {
# deny all;
#}
}
#虚拟主机的设置
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
#加密的http设置
# HTTPS server
#
#server {
# listen 443;
# server_name localhost;
# ssl on;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_timeout 5m;
# ssl_protocols SSLv2 SSLv3 TLSv1;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
nginx高级配置
- worker_processes nginx worker进程数量
- worker_rlimit_nofile 每个worker进程打开的文件最大数量
- worker_connections 每个worker进程允许的最大连接数
- server_tokens 是否显示nginx版本号
- sendfile 优化nginx 读文件,减少上下文切换,把硬盘->kernel buffer ->user buffer -> kernel socket buffer ->协议栈 缩减为 硬盘-> kernel buffer -> 协议栈,原理是直接从kernel buffer 拷贝到了kernel socket buffer
- add_header 在response 的header中自定义key value
- keepalive_timeout nginx和客户端保持连接的时间,默认为75s,0表示不保持连接
- lingering_timeout nginx延迟关闭连接的时间,nginx默认先关闭tcp连接的写,等待一段时间之后再关闭连接的读,延迟服务器发送rst包
- client_header_buffer_size 客户端request大小,包括header和url,超过这个值之后以large_client_header_buffers为最大值
- large_client_header_buffers 客户端request大小,包括header和url,超过这个值之后,将返回414错误
- client_max_body_size 客户端request body大小,限制post数据的大小,超过大小之后返回413错误
- client_body_timeout 定义读取客户端请求正文的超时。超时仅设置在两个连续读取操作之间的时间段,而不是用于传输整个请求体。如果客户端在此时间内未发送任何内容,则会将408(请求超时)错误返回给客户端。
- client_header_timeout定义读取客户端请求标头的超时。如果客户端在此时间内未传输整个头,则会向客户端返回408(请求超时)错误。
- server_names_hash_bucket_size 保存服务器名字的hash表是由指令server_names_hash_max_size 和server_names_hash_bucket_size所控制的。参数hash bucket size总是等于hash表的大小,并且是一路处理器缓存大小的倍数。在减少了在内存中的存取次数后,使在处理器中加速查找hash表键值成为可能。如果hash bucket size等于一路处理器缓存的大小,那么在查找键的时候,最坏的情况下在内存中查找的次数为2。第一次是确定存储单元的地址,第二次是在存储单元中查找键 值。因此,如果Nginx给出需要增大hash max size 或 hash bucket size的提示,那么首要的是增大前一个参数的大小.
- open_file_cache max=65535 inactive=20s; 缓存静态文件,max 指定最大缓存多少个,inactive 指定缓存时间
- open_file_cache_valid 检查静态文件缓存的时间,超过该时间没有命中则删除该静态缓存
- open_file_cache_min_uses 指定时间内使用该缓存的次数,未达到该次数则删除静态缓存
- fastcgi_connect_timeout 指定连接到后端FastCGI的超时时间
- fastcgi_send_timeout指定向FastCGI传送请求的超时时间,这个值是已经完成两次握手后向FastCGI传送请求的超时时间。
- fastcgi_read_timeout指定接收FastCGI应答的超时时间,这个值是已经完成两次握手后接收FastCGI应答的超时时间。
- fastcgi_buffer_size 指定将用多大的缓冲区来读取从FastCGI服务器到来应答的第一部分
- fastcgi_buffers 指定了从FastCGI服务器到来的应答,指定本地需要用多少和多大的缓冲区来缓冲FastCGI的应答请求。如果一个PHP脚本所产生的页面大小为256KB,那么会为其分配4个64KB的缓冲区来缓存;如果页面大小大于256KB,那么大于256KB的部分会缓存到fastcgi_temp指定的路径中
- fastcgi_busy_buffers_size 系统很忙时可以使用的fastcgi_buffers大小,一般fastcgi_buffers的两倍
- fastcgi_temp_file_write_size 128k; 表示在写入缓存文件时使用多大的数据块,默认值是fastcgi_buffers的两倍
- fastcgi_max_temp_file_size 0; 用于配置所有临时文件的总体积大小
- fastcgi_cache 为缓存实际使用的共享内存指定一个区域,相同的区域可以用在不同的地方
- fastcgi_cache_key 设置缓存的关键字
- fastcgi_cache_path path [levels=m:n] keys_zone=name:size [inactive=time] [max_size=size] 指定FastCGI缓存的路径以及其他的一些参数,所有的数据以文件的形式存储,缓存的关键字(key)和文件名为代理的url计算出的MD5值。
- fastcgi_cache_methods 指定缓存的请求类型
- fastcgi_cache_min_uses 指定了经过多少次请求的相同URL将被缓存
- fastcgi_no_cache 定义不缓存的条件
- fastcgi_cache_bypass 定义不缓存的条件
- fastcgi_cache_use_stale 定义那些情况使用过期缓存
- fastcgi_intercept_errors 是否传递
4**
或5**
错误信息到客户端
nginx 调优后的配置文件
user www www;
worker_processes 40;
worker_rlimit_nofile 65535;
error_log /data/logs/nginx/error.log;
pid run/nginx.pid;
events {
use epoll;
worker_connections 65535;
}
http {
server_tokens off;
include mime.types;
default_type application/octet-stream;
sendfile on;
gzip on;
set_real_ip_from 10.0.0.0/8;
set_real_ip_from 172.16.0.0/16;
set_real_ip_from 192.168.0.0/24;
set_real_ip_from 127.0.0.1;
real_ip_header X-Forwarded-For;
map $remote_addr $clientip {
default $remote_addr;
}
log_format main '$http_host $remote_addr ${request_time} - [$time_local] '
'"$request" $status $body_bytes_sent '
'"$http_referer" - "$http_user_agent" "$server_addr"';
add_header XYWY_HEADER $hostname;
keepalive_timeout 0;
lingering_timeout 50ms;
client_header_buffer_size 8k;
client_max_body_size 20m;
client_body_timeout 10s;
client_header_timeout 5s;
large_client_header_buffers 8 4k;
server_names_hash_bucket_size 128;
open_file_cache max=65535 inactive=20s;
open_file_cache_valid 30s;
open_file_cache_min_uses 3;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 128k;
fastcgi_max_temp_file_size 0;
fastcgi_temp_path /dev/shm/nginx_tmp;
fastcgi_cache_path /dev/shm/nginx_cache levels=1:2 keys_zone=common_cache:20m inactive=5m max_size=1024m;
fastcgi_cache_key "$request_method$host$request_uri";
fastcgi_cache_min_uses 1;
fastcgi_cache_bypass $cookie_nocache $arg_nocache;
fastcgi_no_cache $cookie_nocache $arg_nocache;
fastcgi_cache_use_stale error timeout http_500 http_404;
fastcgi_cache_methods GET HEAD POST;
fastcgi_intercept_errors on;