• ngnix 总结


    #user  nobody;
    worker_processes  1;
    
    #error_log  logs/error.log;
    #error_log  logs/error.log  notice;
    #error_log  logs/error.log  info;
    
    #pid        logs/nginx.pid;
    
    
    events {
        worker_connections  1024;
    }
    
    stream {
        upstream backend {
                #ip_hash;
            #hash $uri;
            #hash $consistent_key $arg_cat;
            server  192.168.10.85:8080 max_fails=2 fail_timeout=10s  weight=1;
            server  192.168.10.85:8081 max_fails=2 fail_timeout=10s  weight=1;
        }
    
       
        server {
            listen 81;
            proxy_connect_timeout 1s;
            proxy_timeout 3s;
            proxy_pass backend;
        }
    
        
    }
    
    
    http {
        include       mime.types;
        default_type  application/octet-stream;
    
        #log_format  main  '$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  main;
    
        sendfile        on;
        #tcp_nopush     on;
    
        #keepalive_timeout  0;
        keepalive_timeout  65;
    
        #gzip  on;
    
        #设置缓存
        #proxy_connect_timeout 10;
        #proxy_read_timeout 180;
        #proxy_send_timeout 5;
        proxy_buffer_size 16k;
        proxy_buffers 4 32k;
        proxy_busy_buffers_size 96k;
        proxy_temp_file_write_size 96k;
        proxy_temp_path /tmp/temp_dir;
        proxy_cache_path /tmp/cache levels=1:2 keys_zone=cache_one:100m inactive=1d max_size=10g;
    
    
    
        upstream backend {
        ip_hash;
        #hash $uri;
        #hash $consistent_key $arg_cat;
        server  192.168.10.85:8080 max_fails=2 fail_timeout=10s  weight=1;
        server  192.168.10.85:8081 max_fails=2 fail_timeout=10s  weight=1;
        
          }
    
    
        server {
            listen       80;
            server_name  localhost;
    
            #charset koi8-r;
    
            #access_log  logs/host.access.log  main;
    
            location / {
                #root   html;
                #index  index.html index.htm;
            #设置超时时间
            proxy_connect_timeout 5s;
            proxy_read_timeout 5s;
            proxy_send_timeout 5s;
    
            #设置请求头 Host =客户的ip地址 转发客户的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;
            proxy_set_header Cookie $http_cookie;
     
    
            proxy_next_upstream error timeout;
            proxy_next_upstream_timeout 10s;
            proxy_next_upstream_tries 2;
            proxy_pass  http://backend;
    
            }
    
        
        #要缓存文件的后缀,可以在以下设置。
            location ~ .*.(gif|jpg|png|css|js)(.*) {
                    proxy_pass  http://backend;
                    proxy_redirect off;
                    proxy_set_header Host $host;
                    proxy_cache cache_one;
                    proxy_cache_valid 200 302 24h;
                    proxy_cache_valid 301 30d;
                    proxy_cache_valid any 5m;
                    expires 90d;
                    add_header wall  "hey!guys!give me a star.";
            }
           
            error_page   500 502 503 504  /50x.html;
            location = /50x.html {
                root   html;
            }
            
        }
    }

    1.设置静态图片缓存到本地

     1.1先设置http中设置缓存的名称和配置

      ##cache##
      proxy_connect_timeout 500;
      #跟后端服务器连接的超时时间_发起握手等候响应超时时间
      proxy_read_timeout 600;
      #连接成功后_等候后端服务器响应的时间_其实已经进入后端的排队之中等候处理
      proxy_send_timeout 500;
      #后端服务器数据回传时间_就是在规定时间内后端服务器必须传完所有数据
     proxy_buffer_size 128k;
      #代理请求缓存区_这个缓存区间会保存用户的头信息以供Nginx进行规则处理_一般只要能保存下头信息即可
      proxy_buffers 4 128k;
      #同上 告诉Nginx保存单个用的几个Buffer最大用多大空间
      proxy_busy_buffers_size 256k;
      #如果系统很忙的时候可以申请更大的proxy_buffers 官方推荐*2
      proxy_temp_file_write_size 128k;
      #proxy缓存临时文件的大小
      proxy_temp_path /usr/local/nginx/temp;
      #用于指定本地目录来缓冲较大的代理请求
      proxy_cache_path /usr/local/nginx/cache levels=1:2 keys_zone=cache_one:200m inactive=1d max_size=30g;
      #设置web缓存区名为cache_one,内存缓存空间大小为12000M,自动清除超过15天没有被访问过的缓存数据,硬盘缓存空间大小200g

     1.2 在server中添加路径匹配

      #要缓存文件的后缀,可以在以下设置。
      location ~ .*.(gif|jpg|png|css|js)(.*) {
      proxy_pass http://backend;
      proxy_redirect off;
      proxy_set_header Host $host;
      proxy_cache cache_one;
      proxy_cache_valid 200 302 24h;
      proxy_cache_valid 301 30d;
      proxy_cache_valid any 5m;
      expires 90d;
      add_header wall "hey!guys!give me a star.";
      }

    2.设置ngnix 四层负载 

      四层负载单独模版stream 需要与http 分开

      stream {
      upstream backend {
        #ip_hash;
        #hash $uri;
        #hash $consistent_key $arg_cat;
        server 192.168.10.85:8080 max_fails=2 fail_timeout=10s weight=1;
        server 192.168.10.85:8081 max_fails=2 fail_timeout=10s weight=1;
      }

      server {
        listen 81;
        proxy_connect_timeout 1s;
        proxy_timeout 3s;
        proxy_pass backend;
         }

      }

    3.设置ngnix 负载的算法

      upstream 中添加负载的算法策略

          ip_hash;根据ip地址可以分发

         #hash $uri; 根据url 分发

         weight  默认根据权重分发

      4.设置ngnix 负载多台服务器的健康检查策略

        4.1 ngnix 自带的惰性检查

      server 192.168.10.85:8080 max_fails=2 fail_timeout=10s weight=1;
      server 192.168.10.85:8081 max_fails=2 fail_timeout=10s weight=1;

      在fail_timeout时间内如果有max_fails 次失败 就把此服务器删除转发表

        在fail_timeout后在去检查此服务器  

       4.2商业版提供   或者 淘宝加入的

      ngx_http_upstream_check_module 模版
      该模块可以为Tengine提供主动式后端服务器健康检查的功能。可以直接下载
      Tengine安装
  • 相关阅读:
    openfire 部署后报错: java.lang.IllegalArgumentException: interface xx is not visible from class loader
    Calendar
    list 移除值
    fastjson 返回json字符串,JSON.parse 报错
    tomcat 跨域
    spring boot 笔记
    Mybatis处理列名—字段名映射— 驼峰式命名映射
    hadoop的NullWritable
    CentOS中用Nexus搭建maven私服,为Hadoop编译提供本地镜像
    CentOS中配置xrdp,通过微软远程桌面访问CentOS桌面
  • 原文地址:https://www.cnblogs.com/bug1024/p/10253888.html
Copyright © 2020-2023  润新知