nginx的一般优化



user  nobody;
worker_processes  1;   ###此数跟cpu的核心数是一致的


#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;     ##日志等级 debug | info | notice | warn | error | crit


#pid        logs/nginx.pid;


worker_rlimit_nofile 100000;   ###work进程最大打开文件数限制,也可使用ulimit -n的值,但自己设置的值可突破系统限制。


events {
    worker_connections  1024;   ###一个work进程打开的最大连接数,有worker_rlimit_nofile时,此值可以适当调高。
multi_accept on;   ###告诉nginx收到一个新连接通知后接收到尽可能多的连接
use epoll;   ###用于复用客户端线程轮询方法。linux2.6+应使用epoll。如果你使用*BSD,你应该使用kqueue
}




http {
    server_tokens off;   ###不会让nginx执行更快,但可以关闭在错误页面中nginx的版本数字,有利于安全可以关闭在错误页面中nginx的版本数字,有利于安全
    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"';   
    #access_log  logs/access.log  main;


    sendfile     on;   ##开启高效文件传输模式
autoindex    on;   ##开启目录访问列表,合适下载服务器,默认关闭
    tcp_nopush   on;   ##告诉nginx在一个数据包里发送所有头文件,而不是一个一个发送
tcp_nodelay  on;   ##告诉nginx不要缓存数据,而是一段一段的发送。


    #buffers    重要参数,如果buffer太小,nginx会不停的写一些临时文件,导致磁盘不停去写
    client_body_buffer_size 10k;       #允许客户端请求的最大单个文件字节数
    client_header_buffer_size 1k;        #用于设置客户端请求的Header头缓冲区大小
    client_max_body_size 8m;           #设置客户端上传的文件大小
    large_client_header_buffers 2 1k;  #设置客户端请求的Header头缓冲区大小
    client_header_timeout 10;          #设置请求头的超时时间,设小
client_body_timeout 10;            #设置请求体的超时时间,设小


    keepalive_timeout  65;  #与client保持的连接数,设小
reset_timedout_connection on;   #告诉nginx关闭不响应的客户端连接 
send_timeout  10;  #指定客户端响应超时时间,这个设置不会用于整个转发器,而是在两次客户端读取操作之间。如果在这段时间内,客户端没有读取任何数据,nginx就会关闭连接。


limit_conn_zone $binary_remote_addr zone=addr:5m; ##设置用于保存各种key(比如当前连接数)的共享内存的参数。5m就是5兆字节,这个值应该被设置的足够大以存储(32K*5)32byte状态或者(16K*5)64byte状态。
    limit_conn addr 100;  ##为给定的key设置最大连接数。这里key是addr,我们设置的值是100,也就是说我们允许每一个IP地址最多同时打开有100个连接


include  /etc/nginx/conf.d/*.conf;

    #gzip  on;  #nginx采用gzip压缩的形式发送数据。
    gzip_disable "msie6";  #为指定的客户端禁用gzip功能。此处设置成IE6版本。
gzip_static on;        #告诉nginx在压缩资源之前,先检查是否有预先gzip处理过的资源。
gzip_proxied any;      #允许或禁止压缩基于请求和响应的响应流。
gzip_min_length 1000;  #设置对数据启用压缩的最少字节
gzip_comp_level 4;     #设置数据压缩等级,等级可以是1-9之间的任意数值,9是最慢但是压缩比最大的。我们设置为4,这是一个比较折中的设置。
#压缩数据的类型,还可以添加。
gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;


open_file_cache  max=10000 inactive=20s;  #打开缓存的同时指定缓存最大数目以及时间。我们可以设置一个相对高的最大时间,这样我们可以在它们不活动超过20秒后清除掉。
open_file_cache_valid 30s;    #在open_file_cache中指定检测正确信息的间隔时间。
open_file_cache_min_uses 2;   #定义了open_file_cache中指令参数不活动时间期间里最小的文件数。
open_file_cache_errors on;    #指定了当搜索一个文件时是否缓存错误信息,也包括再次给配置中添加文件。我们也包括了服务器模块,这些是在不同文件中定义的。如果你的服务器模块不在这些位置,你就得修改这一行来指定正确的位置。

######做nginx进行分发处理,负载均衡
upstream name {
    server 10.44.17.249:80 weight=2 max_fails=5 fail_timeout=5s;
        server 10.44.39.10:80  weight=2 max_fails=5 fail_timeout=5s;
        server 10.30.58.54:80  weight=4 max_fails=5 fail_timeout=5s;
}

   
   server {
        listen       80;
        server_name  localhost;


        charset koi8-r;


        #access_log  logs/host.access.log  main;


        #location / {
        #    root   html;
        #    index  index.html index.htm;
        #}
        #############在localtion段进行对图片缓存时间的限制
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ {
     expires 10d;
}


############对/启用反向代理
location / {
    proxy_pass http://127.0.0.1:8081;     ##若有upstream,也可转发到upstream的名字
    proxy_redirect off;
    proxy_set_header X-Real-IP $remote_addr;   
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;   ###后端的web服务器可以通过X-Forwarded-For获取用户真实IP。
    proxy_set_header Host $Host;
#####其他一些的反向代理配置
    client_max_body_size 10m; #允许客户端请求的最大单文件字节数
            client_body_buffer_size 128k; #缓冲区代理缓冲用户端请求的最大字节数,
            proxy_connect_timeout 90; #nginx跟后端服务器连接超时时间(代理连接超时)
            proxy_send_timeout 90; #后端服务器数据回传时间(代理发送超时)
            proxy_read_timeout 90; #连接成功后,后端服务器响应时间(代理接收超时)
            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;
}

        #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
        
#########匹配以php为结尾的
        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;
        }


        #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.conf;
        #}
        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }




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


}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值