nginx文件配置优化

#linzl nginx用户权限 nobody、root
user nobody;
 
#linzl nginx进程数,根据服务器CPU个数设定。
worker_processes 8;
 
#linzl 每个进程打开最大文件描述符数目,可设置为跟操作系统最大打开的文件数量一致(内核linux 2.6以上)。
worker_rlimit_nofile 65535;
 
#linzl 全局错误日志文件,日志输出级别有debug、info、notice、warn、error、crit(类似于Python中的logging)、alert、emerg。
error_log logs/error.log info;
 
#linzl 主进程ID文件路径
pid logs/nginx.pid;
 
events {
    #linzl epoll是多路复用IO(I/O Multiplexing)中的一种方式,仅用于linux2.6以上内核,可以提ngxin的性能。
 #select|poll|kqueue|epoll|resig|/dev/poll|eventport
 #A)标准事件模型
    #Select、poll属于标准事件模型,如果当前系统不存在更有效的方法,nginx会选择select或poll。
    #B)高效事件模型  
 #Kqueue:使用于FreeBSD 4.1+, OpenBSD 2.9+, NetBSD2.0 和 MacOS X. 使用双处理器的MacOS X系统使用kqueue可能会造成内核崩溃。
 #Epoll: 使用于linux内核2.6版本及以后的系统。
 use epoll; 
 
 #linzl 每个进程允许的最多连接数,理论上每台nginx服务器的最大连接数为Maxclient=worker_processes*worker_connections。
    worker_connections 65535;
}
 
#linzl http负载均衡
http {
 #linz 设定mime类型
    include mime.types;
 #linzl 默认文件类型
    default_type application/octet-stream;
 
    #linzl 日志格式(详细)
 #参数 说明 示例
 #$remote_addr 客户端地址 211.28.65.253
 #$remote_user 客户端用户名称 --
 #$time_local 访问时间和时区 18/Jul/2012:17:00:01 +0800
 #$request 请求的URI和HTTP协议 "GET /article-10000.html HTTP/1.1"
 #$http_host 请求地址,即浏览器中你输入的地址(IP或域名) www.wang.com 192.168.100.100
 #$status HTTP请求状态 200
 #$upstream_status upstream状态 200
 #$body_bytes_sent 发送给客户端文件内容大小 1547
 #$http_referer url跳转来源 https://www.baidu.com/
 #$http_user_agent 用户终端浏览器等信息 "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; SV1; GTB7.0; .NET4.0C;
 #$ssl_protocol SSL协议版本 TLSv1
 #$ssl_cipher 交换数据中的算法 RC4-SHA
 #$upstream_addr 后台upstream的地址,即真正提供服务的主机地址 10.10.10.100:80
 #$request_time 整个请求的总时间 0.205
 #$upstream_response_time 请求过程中,upstream响应时间 0.002   
 #log_format main '$remote_addr - $remote_user [$time_local] "$request" '
    # '$status $body_bytes_sent "$http_referer" '
    # '"$http_user_agent" "$http_x_forwarded_for"';
 #linzl 访问日志,需要配合log_format参数使用。
    #access_log logs/access.log main;
    
 #linzl 高效传输文件模式
    sendfile on;
 #tcp_nopush on;
 
    #linzl 超时时间
    keepalive_timeout 65;
 
 #linzl 打开文件制定缓存,默认没有启用,max制定缓存数量,建议和打开文件数一致,inactive是指经过多长时间文件没有被请求后删除缓存。
 open_file_cache max=65535 inactive=60s; 
 #linzl 多长时间检查一次缓存的有效信息
 open_file_cache_valid 80s; 
 #linzl open_file_cache指令中的inactive参数时间内文件的最少使用次数,如果超过此数字,文件描述一致是在缓存中打开的,例如,如果有一个文件再inactive时间内没有被使用,它将被移除缓存。
 open_file_cache_min_uses 1;
    
 #linzl 开启gzip
    #gzip on;
    #gzip_min_length 1k; 
 #处理请求压缩的缓冲区数量和大小。比如32 4K表示按照内存页(one memory page)大小以4K为单位(即一个系统中内存页为4K),申请32倍的内存空间。建议此项不设置,使用默认值。
 #gzip_buffers 32 4k;
 #用于识别http协议的版本,早期的浏览器不支持gzip压缩,用户会看到乱码,所以为了支持前期版本加了此选项。默认在http/1.0的协议下不开启gzip压缩。
 #gzip_http_version 1.0;
 #gzip压缩级别,级别越底压缩速度越快文件压缩比越小,反之速度越慢文件压缩比越大。
 #gzip_comp_level 2;
 #压缩的文件类型
 #gzip_types text/plain application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;
 #是否在http header中添加Vary: Accept-Encoding
 #gzip_vary off;
 #禁用IE 6 gzip
 #gzip_disable "MSIE [1-6]\.";
    
 #linzl http_5678负载均衡
    upstream bjjzmh {
  server 14.27.128.114:5678;
  server 14.27.128.112:5678;
  server 14.27.128.113:5678;
  #linzl 后端服务响应最短时间优先分配请求
  fair;
    }
 #linzl 虚拟主机配置
    server {
  #linzl 监听端口
        listen 82;  
  #linzl 监听主机名称或者IP
        server_name 14.27.128.112;  
  #linzl 字符集
        charset utf-8;  
  #linzl 请求规则
        location / {
            root html;
            index index.html index.htm;
   proxy_pass http://bjjzmh;
            proxy_set_header host $http_host;
   proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }  
  #linzl nginx状态访问页面(需要安装--with-http_stub_status_module模块)
  location /status {
   stub_status on;
  }  
  #linzl 请求错误页面
        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
            root html;
        }
    }
}
原文地址:https://www.cnblogs.com/seamy/p/15648476.html