3.Nginx基本配置详解

基本的 (优化过的)配置javascript

    咱们将修改的惟一文件是nginx.conf,其中包含Nginx不一样模块的全部设置。你应该可以在服务器的/etc/nginx目录中找到nginx.conf。首先,咱们将谈论一些全局设置,而后按文件中的模块挨个来,谈一下哪些设置可以让你在大量客户端访问时拥有良好的性能,为何它们会提升性能。css

高层的配置html

nginx.conf文件中,Nginx中有少数的几个高级配置在模块部分之上。java

  • user www-data;
  • pid /var/run/nginx.pid;
  • worker_processes auto;
  • worker_rlimit_nofile 100000;

userpid应该按默认设置 – 咱们不会更改这些内容,由于更改与否没有什么不一样。node

worker_processes 定义了nginx对外提供web服务时的worder进程数。最优值取决于许多因素,包括(但不限于)CPU核的数量、存储数据的硬盘数量及负载模式。不能肯定的时候,将其设置为可用的CPU内核数将是一个好的开始(设置为“auto”将尝试自动检测它)。nginx

worker_rlimit_nofile 更改worker进程的最大打开文件数限制。若是没设置的话,这个值为操做系统的限制。设置后你的操做系统和Nginx能够处理比“ulimit -a”更多的文件,因此把这个值设高,这样nginx就不会有“too many open files”问题了。web

Events模块json

events模块中包含nginx中全部处理链接的设置。缓存

  • events {
  • worker_connections 2048;
  • multi_accept on;
  • use epoll;
  • }

worker_connections设置可由一个worker进程同时打开的最大链接数。若是设置了上面提到的worker_rlimit_nofile,咱们能够将这个值设得很高。安全

记住,最大客户数也由系统的可用socket链接数限制(~ 64K),因此设置不切实际的高没什么好处。

multi_accept 告诉nginx收到一个新链接通知后接受尽量多的链接。

use 设置用于复用客户端线程的轮询方法。若是你使用Linux 2.6+,你应该使用epoll。若是你使用*BSD,你应该使用kqueue。想知道更多有关事件轮询?看下维基百科吧(注意,想了解一切的话可能须要neckbeard和操做系统的课程基础)

(值得注意的是若是你不知道Nginx该使用哪一种轮询方法的话,它会选择一个最适合你操做系统的)。

HTTP 模块

HTTP模块控制着nginx http处理的全部核心特性。由于这里只有不多的配置,因此咱们只节选配置的一小部分。全部这些设置都应该在http模块中,甚至你不会特别的注意到这段设置。

  • http {
  • server_tokens off;
  • sendfile on;
  • tcp_nopush on;
  • tcp_nodelay on;
  • }

server_tokens 并不会让nginx执行的速度更快,但它能够关闭在错误页面中的nginx版本数字,这样对于安全性是有好处的。

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

tcp_nopush 告诉nginx在一个数据包里发送全部头文件,而不一个接一个的发送

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

  • access_log off;
  • error_log /var/log/nginx/error.log crit;

access_log设置nginx是否将存储访问日志。关闭这个选项可让读取磁盘IO操做更快(aka,YOLO)。

error_log 告诉nginx只能记录严重的错误。

  • keepalive_timeout 10;
  • client_header_timeout 10;
  • client_body_timeout 10;
  • reset_timedout_connection on;
  • send_timeout 10;

keepalive_timeout 给客户端分配keep-alive连接超时时间。服务器将在这个超时时间事后关闭连接。咱们将它设置低些可让ngnix持续工做的时间更长。

client_header_timeout 和client_body_timeout 设置请求头和请求体(各自)的超时时间。咱们也能够把这个设置低些。

reset_timeout_connection告诉nginx关闭不响应的客户端链接。这将会释放那个客户端所占有的内存空间。

send_timeout 指定客户端的响应超时时间。这个设置不会用于整个转发器,而是在两次客户端读取操做之间。若是在这段时间内,客户端没有读取任何数据,nginx就会关闭链接。

  • limit_conn_zone $binary_remote_addr zone=addr:5m;
  • limit_conn addr 100;

limit_conn为给定的key设置最大链接数。这里key是addr,咱们设置的值是100,也就是说咱们容许每个IP地址最多同时打开有100个链接。

limit_conn_zone设置用于保存各类key(好比当前链接数)的共享内存的参数。5m就是5兆字节,这个值应该被设置的足够大以存储(32K*5)32byte状态或者(16K*5)64byte状态。

  • include /etc/nginx/mime.types;
  • default_type text/html;
  • charset UTF-8;

include只是一个在当前文件中包含另外一个文件内容的指令。这里咱们使用它来加载稍后会用到的一系列的MIME类型。

default_type设置文件使用的默认的MIME-type。

charset设置咱们的头文件中的默认的字符集。

如下两点对于性能的提高在伟大的WebMasters StackExchange中有解释。

  • gzip_disable "msie6";

    # gzip_static on;
    gzip_proxied any;
    gzip_min_length 1000;
    gzip_comp_level 4;

    gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;

gzip是告诉nginx采用gzip压缩的形式发送数据。这将会减小咱们发送的数据量。

gzip_disable为指定的客户端禁用gzip功能。咱们设置成IE6或者更低版本以使咱们的方案可以普遍兼容。

gzip_static告诉nginx在压缩资源以前,先查找是否有预先gzip处理过的资源。这要求你预先压缩你的文件(在这个例子中被注释掉了),从而容许你使用最高压缩比,这样nginx就不用再压缩这些文件了(想要更详尽的gzip_static的信息,请点击这里)。

gzip_proxied容许或者禁止压缩基于请求和响应的响应流。咱们设置为any,意味着将会压缩全部的请求。

gzip_min_length设置对数据启用压缩的最少字节数。若是一个请求小于1000字节,咱们最好不要压缩它,由于压缩这些小的数据会下降处理此请求的全部进程的速度。

gzip_comp_level设置数据的压缩等级。这个等级能够是1-9之间的任意数值,9是最慢可是压缩比最大的。咱们设置为4,这是一个比较折中的设置。

gzip_type设置须要压缩的数据格式。上面例子中已经有一些了,你也能够再添加更多的格式。

  • # cache informations about file descriptors, frequently accessed files
    # can boost performance, but you need to test those values
    open_file_cache max=100000 inactive=20s;
    open_file_cache_valid 30s;
    open_file_cache_min_uses 2;
    open_file_cache_errors on;
    ##
    # Virtual Host Configs
    # aka our settings for specific servers
    ##
    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;

 

open_file_cache打开缓存的同时也指定了缓存最大数目,以及缓存的时间。咱们能够设置一个相对高的最大时间,这样咱们能够在它们不活动超过20秒后清除掉。

open_file_cache_valid 在open_file_cache中指定检测正确信息的间隔时间。

open_file_cache_min_uses 定义了open_file_cache中指令参数不活动时间期间里最小的文件数。

open_file_cache_errors指定了当搜索一个文件时是否缓存错误信息,也包括再次给配置中添加文件。咱们也包括了服务器模块,这些是在不一样文件中定义的。若是你的服务器模块不在这些位置,你就得修改这一行来指定正确的位置。

一个完整的配置  

  • user www-data;
    pid /var/run/nginx.pid;
    worker_processes auto;
    worker_rlimit_nofile 100000;

    events {
        worker_connections 2048;
        multi_accept on;
        use epoll;
    }

    http {
        server_tokens off;
        sendfile on;
        tcp_nopush on;
        tcp_nodelay on;

        access_log off;
        error_log /var/log/nginx/error.log crit;

        keepalive_timeout 10;
        client_header_timeout 10;
        client_body_timeout 10;
        reset_timedout_connection on;
        send_timeout 10;

        limit_conn_zone $binary_remote_addr zone=addr:5m;
        limit_conn addr 100;

        include /etc/nginx/mime.types;
        default_type text/html;
        charset UTF-8;

        gzip on;
        gzip_disable "msie6";
        gzip_proxied any;
        gzip_min_length 1000;
        gzip_comp_level 6;
        gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;

        open_file_cache max=100000 inactive=20s;
        open_file_cache_valid 30s;
        open_file_cache_min_uses 2;
        open_file_cache_errors on;

        include /etc/nginx/conf.d/*.conf;
        include /etc/nginx/sites-enabled/*;
    }

实际项目中的配置

ngnx.conf

user www www;
worker_processes auto;

error_log /data/wwwlogs/error_nginx.log crit;
pid /var/run/nginx.pid;
worker_rlimit_nofile 51200;

events {
  use epoll;
  worker_connections 51200;
  multi_accept on;
}

http {
  include mime.types;
  default_type application/octet-stream;
  server_names_hash_bucket_size 128;
  client_header_buffer_size 32k;
  large_client_header_buffers 4 32k;
  client_max_body_size 1024m;
  client_body_buffer_size 10m;
  sendfile on;
  tcp_nopush on;
  keepalive_timeout 120;
  server_tokens off;
  tcp_nodelay on;

  fastcgi_connect_timeout 300;
  fastcgi_send_timeout 300;
  fastcgi_read_timeout 300;
  fastcgi_buffer_size 64k;
  fastcgi_buffers 4 64k;
  fastcgi_busy_buffers_size 128k;
  fastcgi_temp_file_write_size 128k;
  fastcgi_intercept_errors on;

  #Gzip Compression
  gzip on;
  gzip_buffers 16 8k;
  gzip_comp_level 6;
  gzip_http_version 1.1;
  gzip_min_length 256;
  gzip_proxied any;
  gzip_vary on;
  gzip_types
    text/xml application/xml application/atom+xml application/rss+xml application/xhtml+xml image/svg+xml
    text/javascript application/javascript application/x-javascript
    text/x-json application/json application/x-web-app-manifest+json
    text/css text/plain text/x-component
    font/opentype application/x-font-ttf application/vnd.ms-fontobject
    image/x-icon;
  gzip_disable "MSIE [1-6]\.(?!.*SV1)";

  #If you have a lot of static files to serve through Nginx then caching of the files' metadata (not the actual files' contents) can save some latency.
  open_file_cache max=1000 inactive=20s;
  open_file_cache_valid 30s;
  open_file_cache_min_uses 2;
  open_file_cache_errors on;

######################## default ############################
  server {
  listen 80;
  server_name _;
  access_log /data/wwwlogs/access_nginx.log combined;
  root /data/wwwroot/default/Blog;
  index index.html index.htm index.jsp;
  location /nginx_status {
    stub_status on;
    access_log off;
    allow 127.0.0.1;
    deny all;
    }
  location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|mp4|ico)$ {
    expires 30d;
    access_log off;
    }
  location ~ .*\.(js|css)?$ {
    expires 7d;
    access_log off;
    }
  location ~ {
    proxy_pass http://127.0.0.1:8080;
    include proxy.conf;
    }
  location ~ /\.ht {
    deny all;
    }
  }

########################## vhost #############################
  include vhost/*.conf;
}

proxy.conf

proxy_connect_timeout 300s;
proxy_send_timeout 900;
proxy_read_timeout 900;
proxy_buffer_size 32k;
proxy_buffers 4 64k;
proxy_busy_buffers_size 128k;
proxy_redirect off;
proxy_hide_header Vary;
proxy_set_header Accept-Encoding '';
proxy_set_header Referer $http_referer;
proxy_set_header Cookie $http_cookie;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

编辑完配置后,确认重启nginx使设置生效。

  • sudo service nginx restart
相关文章
相关标签/搜索