499689317 / notes

note
2 stars 0 forks source link

nginx参数 #15

Open 499689317 opened 7 years ago

499689317 commented 7 years ago

user nobody;

启动进程数,通常设置与cpu数量相等

worker_processes 1;

全局错误日志记录

error_log logs/error.log; error_log logs/notice.log notice; error_log logs/info.log info;

记录当前启动nginx主进程id

pid logs/nginx.pid;

events { worker_connections 1024; # 单个worker进程的最大并发连接数 }

http { include mime.types; # 指定mime类型,类型由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;
rewrite_log on;

# sendfile 指令指定 nginx 是否调用 sendfile 函数(zero copy 方式)来输出文件,对于普通应用,
# 必须设为 on,如果用来进行下载等应用磁盘IO重负载应用,可设置为 off,以平衡磁盘与网络I/O处理速度,降低系统的uptime.
sendfile        on;
#tcp_nopush     on;

# 连接超时时间
#keepalive_timeout  0;
keepalive_timeout  65;

# gzip压缩开关
#gzip  on;

lua_package_path "/usr/local/openresty/lualib/resty/?.lua;/usr/local/openresty/lualib/resty/upstream/?.lua;;";
# 设置实际服务器列表
# 设置负载均衡服务器列表
upstream load_balance_gate_server {
# weight参数表示权值,权值越大被分配的概率越大
    server 127.0.0.1:3001 weight=1;
    server 127.0.0.1:3002 weight=1;
}

# 引入lua节点健康检查模块
# the size depends on the number of servers in upstream {}:
lua_shared_dict healthcheck 1m;
lua_socket_log_errors off;
init_worker_by_lua_block {
    local hc = require "resty.upstream.healthcheck"
    local ok, err = hc.spawn_checker{
        shm = "healthcheck",  -- defined by "lua_shared_dict"
        upstream = "load_balance_gate_server", -- defined by "upstream"
        type = "http",
        http_req = "GET /game/live HTTP/1.0\r\nHost: load_balance_gate_server\r\n\r\n", -- raw HTTP request for checking
        interval = 2000,  -- run the check cycle every 2 sec
        timeout = 1000,   -- 1 sec is the timeout for network operations
        fall = 3,  -- # of successive failures before turning a peer down
        rise = 2,  -- # of successive successes before turning a peer up
        valid_statuses = {200, 302},  -- a list valid HTTP status code
        concurrency = 1,  -- concurrency level for test requests
    }
    if not ok then
        ngx.log(ngx.ERR, "failed to spawn health checker: ", err)
        return
    end
    -- Just call hc.spawn_checker() for more times here if you have
    -- more upstream groups to monitor. One call for one upstream group.
    -- They can all share the same shm zone without conflicts but they
    -- need a bigger shm zone for obvious reasons.
}

# http服务器
server {
    listen       3000; # 监听3000端口
    server_name  192.168.1.114; # http绑定的ip

    #charset koi8-r;

    #access_log  logs/host.access.log  main;

# 对所有请求进行负载均衡 
    location / {
        root   html; # 定义网站根目录位置
        index  index.html index.htm; # 定义默认首页文件
        proxy_pass  http://load_balance_gate_server; # 请求转向load_balance_server 定义的服务器列表

        #以下是一些反向代理的配置(可选择性配置)
        #proxy_redirect off;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        #后端的Web服务器可以通过X-Forwarded-For获取用户真实IP
        proxy_set_header X-Forwarded-For $remote_addr;
        proxy_connect_timeout 1;          #nginx跟后端服务器连接超时时间(代理连接超时)
        proxy_send_timeout 1;             #后端服务器数据回传时间(代理发送超时)
        proxy_read_timeout 1;             #连接成功后,后端服务器响应时间(代理接收超时)
        proxy_buffer_size 4k;              #设置代理服务器(nginx)保存用户头信息的缓冲区大小
        proxy_buffers 4 32k;               #proxy_buffers缓冲区,网页平均在32k以下的话,这样设置
        proxy_busy_buffers_size 64k;       #高负荷下缓冲大小(proxy_buffers*2)
        proxy_temp_file_write_size 64k;    #设定缓存文件夹大小,大于这个值,将从upstream服务器传

        client_max_body_size 10m;          #允许客户端请求的最大单文件字节数
        client_body_buffer_size 128k;      #缓冲区代理缓冲用户端请求的最大字节数
    }

    #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$ {
    #    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;
    #}
}

# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
#    listen       8000;
#    listen       somename:8080;
#    server_name  somename  alias  another.alias;

#    location / {
#        root   html;
#        index  index.html index.htm;
#    }
#}

# HTTPS server
#
#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;
#    }
#}

}