nginx的核心配置


#user  nobody;
#工作进程,如果CPU大于4的话最好开启2个辅助进程
worker_processes  1;

#nginx的LOG日志输出路径
error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;
worker_rlimit_nofile 115024;

#nginx的主进程号存放的文件路径
pid        logs/nginx.pid;


events {
    #每个nginx的最大连接数
    worker_connections  115024;
}


http {

    include       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"';

    #设置虚拟主机访问的LOG日志
    access_log  logs/access.log;

    #必须设为 on,如果用来进行下载等应用磁盘IO重负载应用,可设置为 off,以平衡磁盘与网络I/O处理速度,降低系统的uptime
    sendfile        on;
    tcp_nopush     on;

    #连接nginx的超时时间,单位为秒
    keepalive_timeout  60;


    #开启gzip压缩功能,只在WEB端提供,后端服务不需要进行开启
    #gzip  on;
    gzip on;
    #设置系统获取几个单位的缓存用于存储gzip的压缩结果数据流。4 16k代表以16k为单位,安装原始数据大小以16k为单位的4倍申请内存
    gzip_buffers 4 16k;
    gzip_types text/plain;
    output_buffers 1 32k;
    #压缩级别,gzip压缩比,1压缩比最小处理速度最快,9压缩比最大但处理速度最慢(传输快但比较消耗cpu)
    gzip_comp_level 2;
    postpone_output 1460;
    #和http头有关系,加个vary头,给代理服务器用的,有的浏览器支持压缩,有的不支持,所以避免浪费不支持的也压缩,所以根据客户端的HTTP头来判断,是否需要压缩
    gzip_vary on;


    #负载均衡的服务器列表

    #upstream www.weinan2087.com {
        #weigth参数表示权值,权值越高被分配到的几率越大,max_fails访问失败的次数,fail_timeout连接超时时间,单位为秒
        #max_fails:允许请求失败的次数默认为1.当超过最大次数时,返回proxy_next_upstream模块定义的错误
        #server 192.168.21.*:7070 weight=3 max_fails=1 fail_timeout=10s;

        #server 192.168.21.*:7055 weight=4 max_fails=1 fail_timeout=10s;
    #}

    #A服务
    server {
        #nginx监听的端口,默认是80,也可以自定义,如果是WEB端并且需要绑定域名的话,必须设置为默认的监听端口
        listen       80;

        #nginx的IP,本地只能写localhost或是域名,不可以写IP
        server_name  localhost;

        #nginx的编码
        charset gb2312;

        #access_log  logs/host.access.log  main;

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


            #proxy_next_upstream http_502 http_404 http_500 http_504 error timeout invalid_header;
            proxy_set_header   Host             $host;
            proxy_set_header   X-Real-IP        $remote_addr;
            proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
            proxy_connect_timeout           10;
            proxy_read_timeout              10;
            proxy_send_timeout              10;
            fastcgi_buffer_size 128k;
            fastcgi_buffers 8 128k;
            proxy_buffer_size  16k;  
            proxy_buffers   4 16k;

            #nignx默认的访问地址,如果打开代理服务器列表,nginx默认的地址不需要打开
            index  index.html index.htm;

            #要进行代理的服务列表名称
            #proxy_pass   http://www.weinan2087.com;
        }

        #定义错误提示页面,50x.html也可以自定义
        error_page   500 502 503 504 404  /50x.html;
        location = /50x.html {
            root   html;
        }


        #静态文件,nginx自己处理
        location ~ ^/(images|javascript|js|css|flash|media)/ {
            root /var/www/virtual/htdocs;
            #过期30天,静态文件不怎么更新,过期可以设大一点,如果频繁更新,则可以设置得小一点。
            expires 30d;
        }

    }


}

原文地址:https://www.cnblogs.com/weinan2087/p/3094221.html