nginx服务器配置说明

 

总结nginx的一些配置选项:

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;



  # 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种方式的分配

  1、轮询(默认)

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

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

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

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

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

 

posted @ 2017-08-10 18:38  残宠魔法袋  阅读(277)  评论(0编辑  收藏  举报