nginx服务器配置说明

 

总结nginx的一些配置选项:javascript

nginx全局配置文件 
# 定义nginx运行的用户和组//一个默认同时为用户和组 //没有则默认为nobody
user www-data;
# nginx进程数,建议设置为cpu核心数量,或者为核心总数的2倍//默认auto则和核心数量相等
worker_processes auto;
# nginx的进程文件
pid /run/nginx.pid;
#一个nginx进程打开的最多文件描述符数目,理论值应该是最多打开文件数(系统的值ulimit -n)与nginx进程数相除,可是nginx分配请求并不均匀,因此建议与ulimit -n的值保持一致。
worker_rlimit_nofile 65535;
 
 

# 工做模式与链接数量上限
events {
  #参考事件模型,use [ kqueue | rtsig | epoll | /dev/poll | select | poll ]; epoll模型是Linux 2.6以上版本内核中的高性能网络I/O模型,若是跑在FreeBSD上面,就用kqueue模型。
    (值得注意的是若是你不知道Nginx该使用哪一种轮询方法的话,它会选择一个最适合你操做系统的)   use epoll;   #单个进程最大链接数(最大链接数=链接数*进程数)   worker_connections 768;   #告诉nginx收到一个新链接通知后接受尽量多的链接。
  multi_accept on; }


# 设定http服务器
http {

  #开启目录列表访问,适合下载服务器,默认关闭。
  autoindex on;
  #开启高效文件传输模式,sendfile指令指定nginx是否调用sendfile函数来输出文件,对于普通应用设为
  on,若是用来进行下载等应用磁盘IO重负载应用,可设置为off,以平衡磁盘与网络I/O处理速度,
  下降系统的负载。注意:若是图片显示不正常把这个改为off。
  sendfile on;
 
 

  # 防止网络阻塞
  tcp_nopush on;
  # 防止网络阻塞
  tcp_nodelay on;
  # http长链接超时时间,单位为秒
  keepalive_timeout 65;


  #设置客户端请求读取超时时间
  client_header_timeout 10;
  #设置客户端请求主体读取超时时间
  client_body_timeout 10;
  # 客户端请求头部的缓冲区大小。这个能够根据你的系统分页大小来设置,通常一个请求头的大小不会超过1k,不过因为通常系统分页都要大于1k,因此这里设置为分页大小。分页大小能够用命令getconf PAGESIZE 取得。
  client_header_buffer_size 4k;
  #用于设置相应客户端的超时时间
  send_timeout 10


  # 这个将为打开文件指定缓存,默认是没有启用的,max指定缓存数量,建议和打开文件数一致,inactive是指通过多长时间文件没被请求后删除缓存。
  open_file_cache max=65535 inactive=60s;
  # 这个是指多长时间检查一次缓存的有效信息。
  open_file_cache_valid 80s; 
  # 语法:open_file_cache_min_uses number 默认值:open_file_cache_min_uses 1 使用字段:http, server, location 这个指令指定了在open_file_cache指令无效的参数中必定的时间范围内可使用的最小文件数,若是使用更大的值,文件描述符在cache中老是打开状态
  open_file_cache_min_uses 1;
  # 语法:open_file_cache_errors on | off 默认值:open_file_cache_errors off 使用字段:http, server, location 这个指令指定是否在搜索一个文件是记录cache错误.
  open_file_cache_errors;


  # proxy_temp_path和proxy_cache_path指定的路径必须在同一分区
  proxy_temp_path /data0/proxy_temp_dir;
  # 设置内存缓存空间大小为200MB,1天没有被访问的内容自动清除,硬盘缓存空间大小为30GB。
  proxy_cache_path /data0/proxy_cache_dir levels=1:2 keys_zone=cache_one:200m inactive=1d max_size=30g;
  types_hash_max_size 2048;
  
  # 并不会让nginx执行的速度更快,但它能够关闭在错误页面中的nginx版本数字,这样对于安全性是有好处的。
  server_tokens;
  server_tokens off;

 

  server_names_hash_bucket_size 64;
  server_name_in_redirect off;


  # 文件扩展名与文件类型映射表
  include /etc/nginx/mime.types;
  # 默认文件类型
  default_type application/octet-stream;

 
 

  # SSL Settings
  # Dropping SSLv3, ref: POODLE
  ssl_protocols TLSv1 TLSv1.1 TLSv1.2;   
  ssl_prefer_server_ciphers on;
  ssl on;
  ssl_certificate /etc/nginx/cert_server/server.crt;
  ssl_certificate_key /etc/nginx/cert_server/server.key;php



  # Logging Settings

  #access_log /var/log/nginx/access.log;
  error_log /var/log/nginx/error.log;

  # Gzip Settings 
  #开启gzip压缩输出
  gzip on;
  gzip_disable "msie6";
  # gzip_proxied any;
  #压缩等级
  # gzip_comp_level 6;
  #压缩缓冲区
  # gzip_buffers 16 8k;
  #压缩版本(默认1.1,前端若是是squid2.5请使用1.0)
  # gzip_http_version 1.1;
  #压缩类型,默认就已经包含text/html,因此下面就不用再写了,写上去也不会有问题,可是会有一个warn。
  # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
  #最小压缩文件大小
  gzip_min_length 1k;
  #开启限制IP链接数的时候须要使用
  #limit_zone crawler $binary_remote_addr 10m;


  # upstream 负载均衡 

  upstream bakend {

    server 127.0.0.1:8027;

    server 127.0.0.1:8028;

    server 127.0.0.1:8029;

    hash $request_uri;

  }

  nginx的upstream目前支持4种方式的分配

  一、轮询(默认)

  每一个请求按时间顺序逐一分配到不一样的后端服务器,若是后端服务器down掉,能自动剔除。

  二、weight
  指定轮询概率,weight和访问比率成正比,用于后端服务器性能不均的状况。
  例如:
  upstream bakend {
    server 192.168.0.14 weight=10;
    server 192.168.0.15 weight=10;
  }

  二、ip_hash
  每一个请求按访问ip的hash结果分配,这样每一个访客固定访问一个后端服务器,能够解决session的问题。
  例如:
  upstream bakend {
    ip_hash;
    server 192.168.0.14:88;
    server 192.168.0.15:80;
  }

  三、fair(第三方)
  按后端服务器的响应时间来分配请求,响应时间短的优先分配。
  upstream backend {
    server server1;
    server server2;
    fair;
  }

  四、url_hash(第三方)

  按访问url的hash结果来分配请求,使每一个url定向到同一个后端服务器,后端服务器为缓存时比较有效。

  例:在upstream中加入hash语句,server语句中不能写入weight等其余的参数,hash_method是使用的hash算法

  upstream backend {
    server squid1:3128;
    server squid2:3128;
    hash $request_uri;
    hash_method crc32;
  }

  tips:

  upstream bakend{#定义负载均衡设备的Ip及设备状态}{
  ip_hash;
  server 127.0.0.1:9090 down;
  server 127.0.0.1:8080 weight=2;
  server 127.0.0.1:6060;
  server 127.0.0.1:7070 backup;
  }
  在须要使用负载均衡的server中增长
  proxy_pass http://bakend/;

  每一个设备的状态设置为:
  1.down表示单前的server暂时不参与负载
  2.weight为weight越大,负载的权重就越大。
  3.max_fails:容许请求失败的次数默认为1.当超过最大次数时,返回proxy_next_upstream模块定义的错误
  4.fail_timeout:max_fails次失败后,暂停的时间。
  5.backup: 其它全部的非backup机器down或者忙的时候,请求backup机器。因此这台机器压力会最轻。

  nginx支持同时设置多组的负载均衡,用来给不用的server来使用。

  client_body_in_file_only设置为On 能够讲client post过来的数据记录到文件中用来作debug
  client_body_temp_path设置记录文件的目录 能够设置最多3层目录

  location对URL进行匹配.能够进行重定向或者进行新的代理 负载均衡


  #虚拟主机的配置
  server{
    #监听端口     listen 80;
    #域名能够有多个,用空格隔开
    server_name www.ha97.com ha97.com;
    index index.html index.htm index.php;     root /data/www/ha97;     location ~ .*\.(php|php5)?${       fastcgi_pass 127.0.0.1:9000;       fastcgi_index index.php;       include fastcgi.conf;     }
    #图片缓存时间设置     location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)${       expires 10d;     }     #JS和CSS缓存时间设置     location ~ .*\.(js|css)?${       expires 1h;     }
    #日志格式设定     log_format access '$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/ha97access.log access;     #对 "/" 启用反向代理
    location / {       proxy_pass http://127.0.0.1:88;       proxy_redirect off;

      proxy_set_header X-Forwarded-Proto $scheme;
      proxy_set_header Host $http_host;
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Real-IP $remote_addr;       #后端的Web服务器能够经过X-Forwarded-For获取用户真实IP       proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;       #如下是一些反向代理的配置,可选。       proxy_set_header Host $host;
      client_max_body_size 10m; #容许客户端请求的最大单文件字节数       client_body_buffer_size 128k; #缓冲区代理缓冲用户端请求的最大字节数,       proxy_connect_timeout 90; #nginx跟后端服务器链接超时时间(代理链接超时)       proxy_send_timeout 90; #后端服务器数据回传时间(代理发送超时)       proxy_read_timeout 90; #链接成功后,后端服务器响应时间(代理接收超时)       proxy_buffer_size 4k; #设置代理服务器(nginx)保存用户头信息的缓冲区大小       proxy_buffers 4 32k; #proxy_buffers缓冲区,网页平均在32k如下的设置       proxy_busy_buffers_size 64k; #高负荷下缓冲大小(proxy_buffers*2)       proxy_temp_file_write_size 64k;       #设定缓存文件夹大小,大于这个值,将从upstream服务器传     }     #设定查看Nginx状态的地址     location /NginxStatus {       stub_status on;       access_log on;       auth_basic "NginxStatus";       auth_basic_user_file conf/htpasswd;       #htpasswd文件的内容能够用apache提供的htpasswd工具来产生。     }     #本地动静分离反向代理配置     #全部jsp的页面均交由tomcat或resin处理     location ~ .(jsp|jspx|do)?$ {       proxy_set_header Host $host;       proxy_set_header X-Real-IP $remote_addr;       proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

      proxy_pass http://127.0.0.1:8080;
    }

    #全部静态文件由nginx直接读取不通过tomcat或resin     location ~ .*.(htm|html|gif|jpg|jpeg|png|bmp|swf|ioc|rar|zip|txt|flv|mid|doc|ppt|pdf|xls|mp3|wma)${
      expires 15d;
    }
    location ~ .*.(js|css)?${
      expires 1h;
    }   }


  include /etc/nginx/conf.d/*.conf;
  include /etc/nginx/sites-enabled/*;
}
 
 
 #mail { # # See sample authentication script at: # # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript # # # auth_http localhost/auth.php; # # pop3_capabilities "TOP" "USER"; # # imap_capabilities "IMAP4rev1" "UIDPLUS"; # # server { # listen localhost:110; # protocol pop3; # proxy on; # } # # server { # listen localhost:143; # protocol imap; # proxy on; # } #}
相关文章
相关标签/搜索