Nginx配置优化

一、Nginx配置文件优化

  1. 设置nginx进程数,推荐按照cpu数目来指定,一般跟cpu核数相同。

    worker_processes 8;

  2. 为每个进程分配cpu,上例中将8个进程分配到8个cpu,当然可以写多个,或者将一个进程分配到多个cpu。

    worker_cpu_affinity 0001 0010 0011 0100 0101 0110 0111 1000;

  3. 配置nginx进程打开的最多文件数目,理论值应该是系统的最多打开文件数(ulimit -n)与nginx进程数相除,但是nginx分配请求并不是那么均匀,所以最好与ulimit -n的值保持一致。

    worker_rlimit_nofile 65535;

  4. 使用epoll的I/O模型,用这个模型来高效处理异步事件

    use epoll;

  5. 每个进程允许的最多连接数,理论上每台nginx服务器的最大连接数为

    worker_processes*worker_connections。

    worker_connections 65535;

  6. http连接超时时间,默认是60s,功能是使客户端到服务器端的连接在设定的时间内持续有效,当出现对服务器的后继请求时,该功能避免了建立或者重新建立连接。切记这个参数也不能设置过大!否则会导致许多无效的http连接占据着nginx的连接数,终nginx崩溃!

    keepalive_timeout 60;

  7. 客户端请求头部的缓冲区大小,这个可以根据你的系统分页大小来设置,一般一个请求的头部大小不会超过1k,不过由于一般系统分页都要大于1k,所以这里设置为分页大小。分页大小可以用命令getconf PAGESIZE取得。

    client_header_buffer_size 4k;

  8. 下面这个参数将为打开文件指定缓存,默认是没有启用的,max指定缓存数量,建议和打开文件数一致,inactive是指经过多长时间文件没被请求后删除缓存。

    open_file_cache max=102400 inactive=20s;

  9. 下面这个是指多长时间检查一次缓存的有效信息。

    open_file_cache_valid 30s;

  10. open_file_cache指令中的inactive参数时间内文件的最少使用次数,如果超过这个数字,文件描述符一直是在缓存中打开的,如上例,如果有一个文件在inactive时间内一次没被使用,它将被移除。

    open_file_cache_min_uses 1;

  11. 隐藏响应头中的有关操作系统和web server(Nginx)版本号的信息,这样对于安全性是有好处的。

    server_tokens off;

  12. 开启零拷贝。可以让sendfile()发挥作用,sendfile()可以在磁盘和TCP socket之间互相拷贝数据(或任意两个文件描述符)。Pre-sendfile是传送数据之前在用户空间申请数据缓冲区。之后用read()将数据从文件拷贝到这个缓冲区,write()将缓冲区数据写入网络。sendfile()是立即将数据从磁盘读到OS缓存。因为这种拷贝是在内核完成的,sendfile()要比组合read()和write()以及打开关闭丢弃缓冲更加有效(更多有关于sendfile)。

    sendfile on;

  13. 告诉nginx在一个数据包里发送所有头文件,而不一个接一个的发送。就是说数据包不会马上传送出去,等到数据包最大时,一次性的传输出去,这样有助于解决网络堵塞。

    tcp_nopush on;

  14. 告诉nginx不要缓存数据,而是一段一段的发送–当需要及时发送数据时,就应该给应用设置这个属性,这样发送一小块数据信息时就不能立即得到返回值。

    tcp_nodelay on;
    比如:
    http {
    server_tokens off;
    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;

    }

  15. 客户端请求头部的缓冲区大小,这个可以根据系统分页大小来设置,一般一个请求头的大小不会超过1k,不过由于一般系统分页都要大于1k,所以这里设置为分页大小。

    client_header_buffer_size 4k;

    客户端请求头部的缓冲区大小,这个可以根据系统分页大小来设置,一般一个请求头的大小不会超过1k,不过由于一般系统分页都要大于1k,所以这里设置为分页大小。
    分页大小可以用命令getconf PAGESIZE取得。
    [root@local~]# getconf PAGESIZE
    4096
    但也有client_header_buffer_size超过4k的情况,但是client_header_buffer_size该值必须设置为“系统分页大小”的整倍数。

  16. 为打开文件指定缓存,默认是没有启用的,max 指定缓存数量,建议和打开文件数一致,inactive 是指经过多长时间文件没被请求后删除缓存。

    open_file_cache max=65535 inactive=60s;

  17. open_file_cache 指令中的inactive 参数时间内文件的最少使用次数,如果超过这个数字,文件描述符一直是在缓存中打开的,如上例,如果有一个文件在inactive 时间内一次没被使用,它将被移除。

    open_file_cache_min_uses 1;

  18. 指定多长时间检查一次缓存的有效信息。

    open_file_cache_valid 80s;

nginx.conf

user root;
worker_processes 8;
worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000;
error_log   /var/log/nginx_error.log;
pid         /usr/local/nginx/nginx.pid;
worker_rlimit_nofile 65535;
 
events {
   use epoll;
   worker_connections 65535;
}
 
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"';
   access_log  /var/log/nginx/access.log  main;
   limit_req_zone $binary_remote_addr zone=test_req:10m rate=10r/s;
   geoip_country /usr/share/GeoIP/GeoIP.dat;
   
   charset   utf-8;
 
   server_names_hash_bucket_size 128;
   client_header_buffer_size 2k;
   large_client_header_buffers 4 4k;
   client_max_body_size 8m;
 
   sendfile on;
   tcp_nopush on;
 
   keepalive_timeout 60;
 
   
   open_file_cache max=204800 inactive=20s;
   open_file_cache_min_uses 1;
   open_file_cache_valid 30s; 
 
   tcp_nodelay on;
   
   gzip on;
   gzip_min_length   1k;
   gzip_buffers     4 16k;
   gzip_http_version 1.0;
   gzip_comp_level 2;
   gzip_types text/plain application/x-javascript text/css application/xml;
   gzip_vary on;
   
   server_tokens off;
   
   server {
     listen       80;
     server_name   www.baidu.com;
     client_max_body_size 300M;
     root   /usr/share/nginx/html;
 
	location /static/ {
            alias /home/Production/Website/static/;
    }
 
    location /manage/ {
        proxy_set_header X-Real-IP $remote_addr;
        proxy_cache nginx-cache;
        proxy_cache_valid 200 10m;
        proxy_pass http://payapi/druid;
    }
 	location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }
    location /api/ {
            limit_req zone=test_req burst=5 nodelay;
            if ($geoip_country_code = "CN") {
                return "404";
            }
            include uwsgi_params;
            uwsgi_pass 0.0.0.0:8000;
    }

 
     location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|js|css)$ {
       expires       30d;
     }
 
     
    }
}

负载均衡

upstream payapi { 
  server 0.0.0.0:8082 fail_timeout=5 max_fails=3;
  server 0.0.0.0:8083 fail_timeout=5 max_fails=3;
  ip_hash;
}
posted @ 2022-03-13 15:15  TY520  阅读(498)  评论(0编辑  收藏  举报