nginx.conf配置文件结构
main
event {}
http{
server {
location /aaa/bbb {
指令;
……
}
}
server {
location /ccc/ddd{
指令;
}
}
}
http{
server {
location /aaa/bbb {
指令;
……
}
}
}
……
语法
1)单条指令由“名称 参数 分号”的形式组成,如:client_body_buffer_size 11M; 注意末尾的分号不能丢
2)块指令有花括号括起来的多个单条指令组成;如:
upstream std {
server 10.***.***.119:8080;
server 10.***.***.120:8080;
check interval=3000 rise=2 fall=5 timeout=1000 type=http;
check_http_send "HEAD /mwmonitor/check.jsp HTTP/1.0\r\n\r\n";
check_http_expect_alive http_2xx;
}
3)块指令里面还可以嵌套块指令
4)不在任何上下文的指令都算作main上下文。events 和http指令在main上下文,server指令在http上下文,location指令在server上下文。
完整示例:
#4个工作进程
worker_processes 4;
#将每个worker绑定到指定的CPU上
worker_cpu_affinity 0001 0010 0100 1000;
#指定error日志输出到/logs/error.log文件中,高于info级别的日志都输出到error.log中
error_log logs/error.log info;
events {
#每个worker最多可以同时连接10240个连接
worker_connections 10240;
}
http {
#定义日志格式
log_format main '$time_iso8601 - info - $server_addr - - - GW - API-GATEWAY - - - - - $http_msgid'
' - $remote_addr - $connection - "$request" - $http_version'
' - $upstream_addr - $status - $body_bytes_sent - $request_time - $upstream_response_time - $distcode - $appkey';
#定义access日志输出路径
access_log logs/access.log main;
#请求body最大大小,对应请求头中“Content-Length”字段
client_max_body_size 11M;
#读取请求body时的buffer大小,默认是2个memory pages,32位系统是8K,64位系统是16K
#如果请求body小于buffer大小,则数据直接放到内存里。否则放到临时文件(client_body_temp指定)
client_body_buffer_size 11M;
lua_shared_dict urlid 3m;
lua_shared_dict ratelimit 2048m;
lua_shared_dict counter 1024m;
lua_shared_dict lock 100k;
lua_shared_dict gray_shared_zone 1m;
include mime.types;
default_type text/plain;
#开启linux kernel的sendfile()功能。用于向客户响应数据发送文件(比如静态文件)时提高性能
#sendfile()在两个文件描述符之间拷贝数据,能快速时间在kerner mode之间拷贝数据
#所以两端都并不能是socket
#所以针对nginx作为静态服务器时能大大提高性能。但是当 Nginx 是作为一个反向代理来使用的时候,SENDFILE 则没什么用了
sendfile on;
#openresty 嵌入的lua代码模块
init_worker_by_lua_block {
local gray_config_file = "../../nginx/conf/gray.json"
local gray_init_worker = require "gray.init_worker"
gray_init_worker.execute(gray_config_file)
local config_file = "../../nginx/conf/gateway.json"
local init_worker = require "gateway.init_worker"
init_worker.execute(config_file)
}
#作为反向代理时,定义一组后端服务器
upstream std {
server 10.***.***.119:8080;
server 10.***.***.120:8080;
server 10.***.***.121:8080;
server 10.***.***.122:8080;
server 10.***.***.123:8080;
server 10.***.***.124:8080;
#健康监测,每3秒监测一次,请求2次正常,则标记real server 为UP;请求5次都失败,则标记real server为down;
check interval=3000 rise=2 fall=5 timeout=1000 type=http;
#健康监测页面
check_http_send "HEAD /mwmonitor/check.jsp HTTP/1.0\r\n\r\n";
#指定HTTP回复的成功状态,默认认为2XX和3XX的状态是健康的。
check_http_expect_alive http_2xx;
}
server {
#监听端口
listen 1080;
set $distcode "-";
set $appkey "-";
#设置virtual server的名称
server_name sfapi-internal.int.sfdc.com.cn;
access_log logs/access.log main;
#request URI的匹配规则,确定相应URI的相应响应。
#当请求URI匹配到/bspsw/std字符串时,
location /bspsw/std {
error_page 502 = @502.json;
error_page 504 = @504.json;
client_max_body_size 102400k;
client_body_buffer_size 102400k;
#关联$cur_proxy_pass变量到前面upstream中std那组后端服务器
set $cur_proxy_pass "std";
#执行相关lua代码的控制逻辑
access_by_lua_block {
local access = require "gateway.access"
access.execute()
local proxy_pass = require "gray.proxy_pass"
ngx.var.cur_proxy_pass = proxy_pass.execute()
}
proxy_http_version 1.1;
#将传给被代理的服务器的请求头中字段进行重定义或者追加
#将请求头中Connection冲定义为""
proxy_set_header Connection "";
#定义从代理的服务器中读取响应的超时时间,默认60秒
proxy_read_timeout 20;
#proxy_next_upstream定义将请求传给下一个服务器的情况,支持error | timeout | invalid_header | http_500 | http_502 | http_503 | http_504 | http_403 | http_404 | http_429 | non_idempotent | off
#这里是关闭这种行为,默认:error timeout时,将请求传给下一个服务器
proxy_next_upstream off;
#定义请求传到哪里去,可以是URL,也可以是linux的socket
#proxy_pass http://unix:/tmp/backend.socket:/uri/;
proxy_pass http://$cur_proxy_pass/bspsw/std?$args;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $http_x_forwarded_for;
}
#将500 502 503 504这几个代码的请求,统一都用/50x.html返回
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}