nginx.conf 配置优化详解

user  www www;——配置nginx运行用户和用户组,使用之前创建用户useradd www -s /sbin/nologin -M
worker_processes 4;——配置nginx worker进程数,根据cpu内核数设置,也可以设置成auto
worker_cpu_affinity 0001 0010 0100 1000;——配置cpu亲和力,此配置为4核(如果cpu是8核,前面worker_processes也设置为8,worker_cpu_affinity配置为00000001,00000010,00000100,00001000,00010000,00100000,01000000,10000000)
worker_rlimit_nofile 65535;——为nginx工作进程改变打开最多文件描述符数目的限制。用来在不重启主进程的情况下增加限制,理论上这个值是最多打开文件数(ulimit -n)与nginx工作进程相除。

events {
    -------设置一下参数之前,保证nginx高并发,检查一下文件句柄打开最大数:ulimit -a 查看open files;设置一下系统内核打开文件句柄:echo "2390251" > /proc/sys/fs/file-max;   sysctl -p
    也可以直接修改vim /etc/security/limits.conf
    *  soft  nofile  65536
    *  hard  nofile  65536  】---------
    worker_connections 65535;——设置nginx服务器的每个工作进程允许同时连接客户端的最大数值,也就是最大连接客户端=worker_processes*worker_connections/2;
    multi_accept on;——告诉nginx收到一个新连接通知后接受尽可能多的连接
    use epoll;——使用epoll的I/O模型

}

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" '
                    '$upstream_addr $upstream_response_time $request_time';
    access_log logs/access.log main;
    sendfile on;——设置为on表示启动高效传输文件的模式
    tcp_nopush on;——必须在sendfile开启模式才有效,防止网路阻塞,积极的减少网络报文段的数量
    server_tokens off;——不显示nginx版本信息
    keepalive_timeout 65;——长连接timeout
    upstream interface{
    server 192.168.0.1:8089;——反向代理上游服务器
    server 192.168.0.2:8089;
}

    server {
        listen       8080;——本服务器侦听端口,要注意的是普通用户是无法启动80端口的;要不修改成其他端口,要不就把sbin/nginx修改成chown root nginx,然后chmod u+s nginx 再启动nginx;
        server_name  192.168.0.3;
        access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
            proxy_connect_timeout 90;——该指令设置与upstream server的连接超时时间,有必要记住,这个超时不能超过75秒
        proxy_send_timeout 90;——这个指定设置了发送请求给upstream服务器的超时时间。
            proxy_read_timeout 90;——该指令设置与代理服务器的读超时时间。它决定了nginx会等待多长时间来获得请求的响应
            proxy_pass http://interface;

        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

猜你喜欢

转载自blog.51cto.com/12185273/2126805
今日推荐