nginx 配置优化详解

# nginx不同于apache服务器,当进行了大量优化设置后会魔术般的明显性能提升效果
# nginx在安装完成后,大部分参数就已经是最优化了,我们需要管理的东西并不多

#user  nobody;

#阻塞和非阻塞网络模型:
#同步阻塞模型,一请求一进(线)程,当进(线)程增加到一定程度后
#更多CPU时间浪费到切换一,性能急剧下降,所以负载率不高
#Nginx基于事件的非阻塞多路复用(epoll或kquene)模型
#一个进程在短时间内可以响应大量的请求
#建议值 <= cpu核心数量,一般高于cpu数量不会带好处,也许还有进程切换开销的负面影响
worker_processes 4;

#将work process绑定到特定cpu上,避免进程在cpu间切换的开销
worker_cpu_affinity 0001 0010 0100 1000 
#4内核4进程时的设置方法
#8内核4进程时的设置方法 worker_cpu_affinity 00000001 00000010 00000100 10000000
 
# 每进程最大可打开文件描述符数量(linux上文件描述符比较广义,网络端口、设备、磁盘文件都是)
# 文件描述符用完了,新的连接会被拒绝,产生502类错误
# linux最大可打开文件数可通过ulimit -n FILECNT或 /etc/security/limits.conf配置
# 理论值 系统最大数量 / 进程数。但进程间工作量并不是平均分配的,所以可以设置的大一些
worker_rlimit_nofile 655350 

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;
#pid        logs/nginx.pid;


events {
    # 并发响应能力的关键配置值
	# 每个进程允许的最大同时连接数,work_connectins * worker_processes = maxConnection;
	# 要注意maxConnections不等同于可响应的用户数量,
	# 因为一般一个浏览器会同时开两条连接,如果反向代理,nginx到后端服务器的连接也要占用连接数
	# 所以,做静态服务器时,一般 maxClient = work_connectins * worker_processes / 2
	# 做反向代理服务器时 maxClient = work_connectins * worker_processes / 4
	
	# 这个值理论上越大越好,但最多可承受多少请求与配件和网络相关,也可最大可打开文件,最大可用sockets数量(约64K)有关
    worker_connections  500;
	
	# 指明使用epoll 或 kquene (*BSD)
	use epoll
	
	# 备注:要达到超高负载下最好的网络响应能力,还有必要优化与网络相关的linux内核参数
}


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

	# 关闭此项可减少IO开销,但也无法记录访问信息,不利用业务分析,一般运维情况不建议使用
	access_log off
	# 只记录更为严重的错误日志,可减少IO压力
	error_log logs/error.log crit;
    #access_log  logs/access.log  main;

	# 启用内核复制模式,应该保持开启达到最快IO效率
    sendfile        on;
	
	# 简单说,启动如下两项配置,会在数据包达到一定大小后再发送数据
	# 这样会减少网络通信次数,降低阻塞概率,但也会影响响应及时性
	# 比较适合于文件下载这类的大数据包通信场景
    #tcp_nopush     on; 在 
	#tcp_nodelay on|off on禁用Nagle算法 

    #keepalive_timeout  0;
	
	# HTTP1.1支持持久连接alive
	# 降低每个连接的alive时间可在一定程度上提高可响应连接数量,所以一般可适当降低此值
    keepalive_timeout  30s;

	# 启动内容压缩,有效降低网络流量
    gzip on;	
	# 过短的内容压缩效果不佳,压缩过程还会浪费系统资源
 	gzip_min_length 1000;
	# 可选值1~9,压缩级别越高压缩率越高,但对系统性能要求越高
	gzip_comp_level 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=655350 inactive=20s;
	# 验证缓存有效期时间间隔
	open_file_cache_valid 30s;
	# 有效期内文件最少使用次数
	open_file_cache_min_uses 2;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

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

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

}

  

原文地址:https://www.cnblogs.com/zyw-205520/p/5078003.html