zoukankan      html  css  js  c++  java
  • Nginx常用配置整理

    1.全局块:配置影响nginx全局的指令。一般有运行nginx服务器的用户组,nginx进程pid存放路径,日志存放路径,配置文件引入,允许生成worker process数等。

    worker_processes 6;    //worker进程数(一般设为CPU核数)

    worker_cpu_affinity 000001 000010 000100 001000 010000 100000;    //每个worker进程对应的CPU(每一位代表一个核,位值为1代表开启,为0代表关闭)

    worker_rlimit_nofile 50000; //设置worker进程的最大打开文件数

    2.events块:配置影响nginx服务器或与用户的网络连接。有每个进程的最大连接数,选取哪种事件驱动模型处理连接请求,是否允许同时接受多个网路连接,开启多个网络连接序列化等。

    use epoll;    //使用epoll的I/O模型。(分为标准事件模型(Select、poll)和高效事件模型(Kqueue、Epoll、/dev/poll、/dev/poll))
    worker_connections 50000; //工作进程的最大连接数量(理论上每台nginx服务器的最大连接数为worker_processes*worker_connections)

    3.http块:可以嵌套多个server,配置代理,缓存,日志定义等绝大多数功能和第三方模块的配置。如文件引入,mime-type定义,日志自定义,是否使用sendfile传输文件,连接超时时间,单连接请求数等。

      server块:配置虚拟主机的相关参数,一个http中可以有多个server。

      location块:配置请求的路由,以及各种页面的处理情况,一个server中可以有多个location。

    include       mime.types;
    default_type application/octet-stream;

    关于mime type,HTTP request里面有一个头叫 Accept,列出浏览器可以接受的mime type,HTTP response 的Content-Type 的值 在Accept 里面。我的理解是Nginx 会根据请求的文件的扩展名来决定返回什么 Content-Type,除非后端Web程序手动设置了Content-Type,如果Web程序没设置,Nginx也没找到对应文件的扩展名的话,就使用默认的Type,这个在Nginx 里用 default_type定义,比如 default_type  application/octet-stream。mime type 和 文件扩展名的对应关系一般放在 mime.types 里,然后 用 include mime.types; 加载

    underscores_in_headers on; 
    #该属性默认为off,表示如果header name中包含下划线,则忽略掉(后端服务器就获取不到带_的参数了)。
    sendfile on;

    tcp_nopush on;

    sendfile: 设置为on表示启动高效传输文件的模式。sendfile可以让Nginx在传输文件时直接在磁盘和tcp socket之间传输数据。如果这个参数不开启,会先在用户空间(Nginx进程空间)申请一个buffer,用read函数把数据从磁盘读到cache,再从cache读取到用户空间的buffer,再用write函数把数据从用户空间的buffer写入到内核的buffer,最后到tcp socket。开启这个参数后可以让数据不用经过用户buffer。

    在 nginx 中,tcp_nopush 配置和 tcp_nodelay "互斥"。它可以配置一次发送数据的包大小。也就是说,它不是按时间累计 0.2 秒后发送包,而是当包累计到一定大小后就发送。必须和 sendfile 搭配使用。

    # 日志格式

    log_format main '$time_iso8601 $status $connection $connection_requests $remote_addr $http_x_forwarded_for $remote_user $request_length $request_time $request_method $server_protocol $http_host $server_port $uri $args $http_referer $body_bytes_sent $http_user_agent $ssl_protocol $ssl_cipher $upstream_addr $upstream_status $upstream_response_time $upstream_cache_status';
    
    log_format time '$remote_addr $time_local $request_time $http_host $request $status';
    # 处理时间,如果有文件上传业务,需要设置长一点。
    keepalive_timeout 5;
    # 用户请求头的超时时间
    client_header_timeout 1m;
    
    # 用户请求体的超时时间
    client_body_timeout 1m;
    # 用户请求体最大字节数
    client_max_body_size 10m;
    
    fastcgi_connect_timeout 60;
    fastcgi_send_timeout 60;
    fastcgi_read_timeout 60;
    
    #这个不能大小 太小会常出502错误
    fastcgi_buffer_size 256k;
    fastcgi_buffers 8 256k;
    fastcgi_busy_buffers_size 256k;
    fastcgi_temp_file_write_size 256k;
    # fastcgi_temp_path /dev/shm;
    # fastcgi_intercept_errors on;
    
    #多长时间检查一次缓存的有效信息
    open_file_cache_min_uses 1;
    open_file_cache_valid 30s;
    
     
    #缓存配置
    proxy_temp_path /data/nginx/temp_dir;
    proxy_cache_path /dev/shm/nginx_cache levels=1:2 keys_zone=cache_one:50m inactive=12d max_size=7g;
    proxy_cache_use_stale error timeout invalid_header updating http_500 http_502 http_503 http_504 http_404 ;
    
     
    #gzip相关配置
    gzip on;
    gzip_min_length 4k;
    gzip_buffers 4 16k;
    gzip_http_version 1.1;
    gzip_comp_level 2;
    gzip_types text/plain application/x-javascript text/css application/xml application/json;
    gzip_vary on;
    
     
    
    #添加if_modified_since规则
    if_modified_since before;
    
    
    limit_req_zone $binary_remote_addr zone=perip_req:1m rate=5r/s;
    #导入其他配置文件,这里可以针对不同业务场景配置不同server include vhosts/*.com; include vhosts/*.conf; include vhosts/*.cn; #隐藏nginx版本信息 server_tokens off; #禁用空主机头访问 server {   listen 80 default;   return 403; }
  • 相关阅读:
    存储过程VMMS—审核
    简单之美—软件开发实践者的思考—故事场景3
    win8 去除快捷方式小箭头
    windows 下版本控制系统 安装与 配置
    win8 开机项的 删除
    Fedora18 KDE 的安装
    new FileOutputStream(fileName)时出现 FileNotFoundException
    eclipse的一些优化知识
    阿里
    常见算法题
  • 原文地址:https://www.cnblogs.com/hechao123/p/9201054.html
Copyright © 2011-2022 走看看