上篇文章简单搭建了一个nginx test案例,以下介绍下nginx.conf配置文件基本信息:
nginx.conf配置文件:
#user root;#定义Nginx运行的用户和用户组
worker_processes auto;#表示工作进程的数量,一般设置为cpu的核数
#worker_rlimit_nofile 65535;#指定一个nginx进程可以打开的最多文件描述符数目
#全局错误日志及PID文件,warn是错误日志级别
#错误日志定义等级,[ debug | info | notice | warn | error | crit ]
error_log logs/error.log warn;
#pid logs/nginx.pid;#log日志进程文件
#工作模式及连接数上限
events {
worker_connections 51200;#表示每个工作进程的最大连接数
multi_accept on;#尽可能多的接受请求
#use epoll;#epoll是多路复用IO(I/O Multiplexing)中的一种方式,但是仅用于linux2.6以上内核,可以大大提高nginx的性能
}
#设定http服务器,利用它的反向代理功能提供负载均衡支持
http {
include mime.types;#设定mime类型,类型由mime.type文件定义
default_type text/html;#请求默认返回格式
#设定日志格式
log_format main '$remote_addr\t$http_x_forwarded_for\t-\t$remote_user\t$time_iso8601\t$request_method\t"$document_uri"\t"$query_string"\t$server_protocol\t$status\t$body_bytes_sent\t$request_time\t"$http_referer"\t"$http_user_agent"\t$http_Cdn-Src-Ip\t$host\t$hostname\t$server_addr\t-\t-\t-\t-\t-\tV5';
access_log logs/access.log main; #设定本虚拟主机的访问日志
sendfile on;#指定nginx是否调用sendfile函数(zero copy 方式)来输出文件,对于普通应用必须设为 on
tcp_nopush on; #防止网络阻塞
tcp_nodelay on; #提高数据的实时响应性
#超时时间,客户端到服务器端的连接持续有效时间,单位是秒
keepalive_timeout 30;
gzip on;#开启gzip压缩
gzip_vary on;
gzip_min_length 1k;
gzip_buffers 4 8k;
gzip_comp_level 1;#压缩级别大小,最大为9,值越小压缩后比例越小CPU处理更快,值越大消耗CPU比较高。
gzip_types text/plain application/x-javascript text/css text/htm application/xml application/javascript text/javascript;
gzip_http_version 1.1;
proxy_buffering off;
#打开文件指定缓存,默认是没有启用的,max指定缓存数量,建议和打开文件数一致,inactive是指经过多长时间文件没被请求后删除缓存。
open_file_cache max=51200 inactive=20s;
#多长时间检查一次缓存的有效信息。
open_file_cache_valid 30s;
#open_file_cache指令中的inactive参数时间内文件的最少使用次数,如果超过这个数字,文件描述符一直是在缓存中打开的,如上例,如果有一个文件在inactive
open_file_cache_min_uses 2;
open_file_cache_errors on;
lua_shared_dict logCache 100m;#缓存
#lua模块路径,多个之间”;”分隔,其中”;;”表示默认搜索路径。windows下相对地址,linux下绝对路径
lua_package_path 'src/?.lua;;src/app/?.lua';
#lua_package_cpath '/usr/local/lib/lua/5.1/?.so;/usr/local/lib/?.so;;';#C语言依赖库
#初始化脚本:windows下相对地址,linux下绝对路径
init_by_lua_file src/init.lua;
#负载均衡配置,tdt_wugk是负载均衡名称
#upstream tdt_wugk {
#server 127.0.0.1:8080 weight=1 max_fails=2 fail_timeout=30s;
#server 127.0.0.1:8081 weight=1 max_fails=2 fail_timeout=30s;
#}
#虚拟主机配置
server {
listen 80;#监听端口,默认80端口号
server_name dev.cnsuning.com;#监听域名
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
set $APP_PATH ''; #设置app请求路径
#通用匹配,任何未匹配到其它location的请求都会匹配到,相当于switch中的default
location / {
root html;#指定对应uri的资源查找路径,这里html为相对路径;
index index.html index.htm;#指定首页index文件的名称,可以配置多个以空格分开,如有多个按配置顺序查找。
}
#精确匹配
location = /favicon.ico {
log_not_found off;
}
#设定查看Nginx状态的地址
#不带任何修饰符前缀匹配,但是在正则匹配之后
location /Nginx-status {
stub_status on;
access_log off;
}
#不带任何修饰符前缀匹配,但是在正则匹配之后
location /sumis-web/ {
internal;#此处表示属于内部调用,外部无法直接访问
#echo_after_body "$host";
#proxy_buffering off;
#proxy_set_header Host mois.suning.com;
#proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
#proxy_redirect off;
proxy_connect_timeout 1;
#proxy_set_header Connection "";
#proxy_http_version 1.1;
#proxy_send_timeout 30;
#proxy_read_timeout 30;
proxy_set_header Accept-Encoding "";
#proxy_pass_request_headers off;
proxy_pass http://sumfsdev.cnsuning.com;
}
#^~ 开头对URL路径进行前缀匹配并且在正则之前。
location ^~ /api/ {
charset UTF-8;
default_type text/html;
set $ROUTE_PATH 'route';
content_by_lua_file '${APP_PATH}/intfMerge/lua_star.lua';
}
#定义错误提示页面
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
localtion匹配规则:
语法规则:location [=|~|~*|^~] /uri/ { … }
模式 | 含义 |
---|---|
location = /uri | = 表示精确匹配,只有完全匹配上才能生效 |
location ^~ /uri | ^~ 开头对URL路径进行前缀匹配,并且在正则之前。 |
location ~ pattern | 开头表示区分大小写的正则匹配 |
location ~* pattern | 开头表示不区分大小写的正则匹配 |
location /uri | 不带任何修饰符前缀匹配,但是在正则匹配之后 |
location / | 通用匹配,任何未匹配到其它location的请求都会匹配到,相当于switch中的default |
error_log 指令用来指定错误日志,语法: error_log path [level];
其中path表示错误日志存放路径,level 表示错误日志等级,
日志等级包括 debug、info、notice、warn、error、crit、alert、emerg,从左至右日志详细程度逐级递减,即debug最详细,emerg最少,默认为error。