kevin0490 发表于 2018-11-15 13:30:27

nginx配置学习

  user nginx nginx;
  worker_process 4;//cpu core
  error_log logs/error.log notice;//debug info warn error crit
  pid logs/nginx.pid;
  worker_rlimit_nofile 65535;
  events {
  use epoll;//linux 下用epoll 。unix系统下一班用kquque ,原因不知
  worker_connections 65535;
  }
  http {
  include mime.types;
  default_type application/octet-stream;
  log_format nginx '$remote_addr - $remote_user [$time_local] "$request" '
  '$status $body_bytes_sent "$http_referer" '
  ' "$http_user_agent" "$http_x_forwarded_for" ';
  access_log logs/access.log nginx;
  ###########
  client_max_body_size 20m;//请求头字段中指定的客户端请求主体,设置允许的最大大小。如果超过设定的值,客户端错误413(请求实体过大),则返回
  client_header_buffer_size 32k;//指令设置headerbuffer的来自客户端的请求标头的大小。
  client_body_temp_path /data/nginx/temp 1 2;
  server_names_hash_bucket_size 128;//name server 的长度设置
  large_client_header_buffers 4 32k;
  #请求行不能大于一个缓冲区的大小,如果客户端发送一个更大的头nginx的返回错误“请求URI过大”(414)。
  #最长的标题行的要求也必须是不超过一个缓冲区的大小,否则客户端将得到错误“错误的请求”(400)。
  sendfile on;
  tcp_nodelay on;
  tcp_nopush on;
  keepalive_timeout 60;
  #
  client_body_buffer_size512k;#缓冲去代理用户请求的最大字节数,可以理解为先保存本地,然后在传给用户
  proxy_connect_timeout    60;#代理连接超时时间 官方建议不要超过75秒 默认为60秒
  proxy_read_timeout       60;#从代理服务器读取回复的超时时间
  proxy_send_timeout       60;#发送到代理服务器的超时时间
  proxy_buffer_size      16k; #代理的buffer大小用户缓存代理回复的header 其大小由proxy_buffers指令指定
  proxy_buffers            4 64k;
  proxy_busy_buffers_size 128k;#高负载下缓冲大小
  proxy_temp_file_write_size 128k; //proxy缓存临时文件的大小
  #
  gzip on;
  gzip_min_length1k;
  gzip_buffers   4 16k;
  gzip_http_version 1.1;
  gzip_comp_level 2;
  gzip_types       text/plain application/x-javascript text/css application/xml;
  gzip_vary on;
  proxy_temp_path   /data/proxy_temp_dir;
  #设置Web缓存区名称为cache_one,内存缓存空间大小为200MB,1天没有被访问的内容自动清除,硬盘缓存空间大小为30GB。
  proxy_cache_path/data/proxy_cache_dirlevels=1:2   keys_zone=cache_one:200m inactive=1d max_size=30g;
  #Starting from version 0.8.9 temporary files and the cache can be put on different file systems but be aware that in this case a file #is copied across two file systems instead of the cheap rename operation. It is thus recommended that for any given location both #cache and a directory holding temporary files set by the proxy_temp_path directive are put on the same file system.
  upstream backend_server {
  server   192.168.0.251:80 weight=1 max_fails=2 fail_timeout=30s;定义server的地址和端口 以及权重 和最大失败次数和失败的超时时间
  server   192.168.0.252:80 weight=1 max_fails=2 fail_timeout=30s;
  server   192.168.0.253:80 weight=1 max_fails=2 fail_timeout=30s;
  } upstream 中的参数
  #iphash
  #keepalive connections
  ######
  server
  {
  listen       80;
  server_name192.168.0.20;
  index index.html index.htm;
  root/data/htdocs/www;
  location /
  {
  #如果后端的服务器返回502、504、执行超时等错误,自动将请求转发到upstream负载均衡池中的另一台服务器,实现故障转移。
  proxy_next_upstream http_502 http_504 error timeout invalid_header;
  proxy_cache cache_one;
  #对不同的HTTP状态码设置不同的缓存时间
  proxy_cache_valid200 304 12h;
  #以域名、URI、参数组合成Web缓存的Key值,Nginx根据Key值哈希,存储缓存内容到二级缓存目录内
  proxy_cache_key $host$uri$is_args$args;
  proxy_set_header Host$host;
  proxy_set_header X-Forwarded-For$remote_addr;
  proxy_pass http://backend_server;
  expires      1d;
  }
  location ~ /purge(/.*)
  {

[*]#设置只允许指定的IP或IP段输入正确的密码才可以清除URL缓存。


[*]auth_basic “Please Insert User And Password”;
  auth_basic_user_file /usr/local/nginx/conf/htpasswd;
  allow 127.0.0.1;
  allow 192.168.1.0/24;
  deny all;
  proxy_cache_purge cache_one $host$1$is_args$args;//http://labs.frickle.com/nginx_ngx_cache_purge/README
  }
  }
  ####
  ..........
  深入的学习nginx,博客当做是自己学习的记录。。。发上来和各位分享。。

页: [1]
查看完整版本: nginx配置学习