Nginx

Ngin

官网:http://www.nginx.org/

Nginx的功能:

  1. HTTP代理,反向代理

  2. 负载均衡

  3. web缓存

配置文件:conf/nginx.conf

#   表示注释符号

nginx配置文件解析

#...        #全局块
#user nobody; # 配置用户或者组,默认为nobody nobody。
worker_processes 1; # 允许生成的进程数,默认为1

#error_log logs/error.log; # 制定日志路径,级别。这个设置可以放入全局块,http块,server块,级别以此为:debug|info|notice|warn|error|crit|alert|emerg
#error_log logs/error.log notice;
#error_log logs/error.log info;

#pid       logs/nginx.pid; # 指定nginx进程运行文件存放地址

#...        
#events块
events {
  accept_mutex on; # 设置网路连接序列化,防止惊群现象发生,默认为on
  multi_accept on; # 设置一个进程是否同时接受多个网络连接,默认为off
  #use epoll;     # 事件驱动模型,select|poll|kqueue|epoll|resig|/dev/poll|eventport
  worker_connections 1024;   # 最大连接数,默认为512
}


#...        
#http块
http {

  include       mime.types; #文件扩展名与文件类型映射表
   
  default_type application/octet-stream; #默认文件类型,默认为text/plain
   
  #access_log off; #取消服务日志
   
  #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 log/access.log myFormat; #combined为日志格式的默认值
   
  sendfile on; #允许sendfile方式传输文件,默认为off,可以在http块,server块,location块
   
  sendfile_max_chunk 100k; #每个进程每次调用传输数量不能大于设定的值,默认为0,即不设上限
   
  keepalive_timeout 65; #连接超时时间,默认为75s,可以在http,server,location块

  upstream mysvr {  
    server 127.0.0.1:7878;
    server 192.168.10.121:3333 backup; #热备
  }
  error_page 404 https://www.baidu.com; #错误页
   
  #tcp_nopush     on;

  #gzip on;

#...        
#server块
server {
      keepalive_requests 120; #单连接请求上限次数。
      listen       4545;   #监听端口
      server_name 127.0.0.1;   #监听地址  
       
      location ~*^.+$ {       #请求的url过滤,正则匹配,~为区分大小写,~*为不区分大小写。
          #root path; #根目录
          #index vv.txt; #设置默认页
          proxy_pass http://mysvr; #请求转向mysvr 定义的服务器列表
          deny 127.0.0.1; #拒绝的ip
          allow 172.18.5.54; #允许的ip          
      }
  }
   
  server {  
      listen       80;
      server_name localhost;
      #charset koi8-r;
      #access_log logs/host.access.log main;
       
      location / {
          root   html;
          index index.html index.htm;
      }

      #error_page 404             /404.html;

      # redirect server error pages to the static page /50x.html
      #
      error_page   500 502 503 504 /50x.html;
      location = /50x.html {
          root   html;
      }

      # proxy the PHP scripts to Apache listening on 127.0.0.1:80
      #
      #location ~ \.php$ {
      #   proxy_pass   http://127.0.0.1;
      #}

      # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
      #
      #location ~ \.php$ {
      #   root           html;
      #   fastcgi_pass   127.0.0.1:9000;
      #   fastcgi_index index.php;
      #   fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
      #   include       fastcgi_params;
      #}

      # deny access to .htaccess files, if Apache's document root
      # concurs with nginx's one
      #
      #location ~ /\.ht {
      #   deny all;
      #}
  }


  # another virtual host using mix of IP-, name-, and port-based configuration
  #
  #server {
  #   listen       8000;
  #   listen       somename:8080;
  #   server_name somename alias another.alias;

  #   location / {
  #       root   html;
  #       index index.html index.htm;
  #   }
  #}


  # HTTPS server
  #
  #server {
  #   listen       443 ssl;
  #   server_name localhost;

  #   ssl_certificate     cert.pem;
  #   ssl_certificate_key cert.key;

  #   ssl_session_cache   shared:SSL:1m;
  #   ssl_session_timeout 5m;

  #   ssl_ciphers HIGH:!aNULL:!MD5;
  #   ssl_prefer_server_ciphers on;

  #   location / {
  #       root   html;
  #       index index.html index.htm;
  #   }
  #}

}
  1. 1、$ $remote_addr与$http_x_forwarded_for 用以记录客户端的ip地址;

    2.$remote_user :用来记录客户端用户名称;

    3.$time_local : 用来记录访问时间与时区;

    4.$request : 用来记录请求的url与http协议;

    5.$status : 用来记录请求状态;成功是200,

    6.$body_bytes_s ent :记录发送给客户端文件主体内容大小;

    7.$http_referer :用来记录从那个页面链接访问过来的;

    8.$http_user_agent :记录客户端浏览器的相关信息;

  2. 惊群现象:一个网路连接到来,多个睡眠的进程被同事叫醒,但只有一个进程能获得链接,这样会影响系统性能。

  3. 每个指令必须有分号结束。

 

#全局块

#...        #全局块
#user nobody; # 配置用户或者组,默认为nobody nobody。
worker_processes 1; # 允许生成的进程数,默认为1

#error_log logs/error.log; # 制定日志路径,级别。这个设置可以放入全局块,http块,server块,级别以此为:debug|info|notice|warn|error|crit|alert|emerg
#error_log logs/error.log notice;
#error_log logs/error.log info;

#pid       logs/nginx.pid; # 指定nginx进程运行文件存放地址

 

#events块

#...        
#events块
events {
  accept_mutex on; # 设置网路连接序列化,防止惊群现象发生,默认为on
  multi_accept on; # 设置一个进程是否同时接受多个网络连接,默认为off
  #use epoll;     # 事件驱动模型,select|poll|kqueue|epoll|resig|/dev/poll|eventport
  worker_connections 1024;   # 最大连接数,默认为512
}


 

#server块


#...        
#server块
server {
      keepalive_requests 120; #单连接请求上限次数。
      listen       4545;   #监听端口
      server_name 127.0.0.1;   #监听地址  
       
      location ~*^.+$ {       #请求的url过滤,正则匹配,~为区分大小写,~*为不区分大小写。
          #root path; #根目录
          #index vv.txt; #设置默认页
          proxy_pass http://mysvr; #请求转向mysvr 定义的服务器列表
          deny 127.0.0.1; #拒绝的ip
          allow 172.18.5.54; #允许的ip          
      }
  }
   
  server {  
      listen       80;
      server_name localhost;
      #charset koi8-r;
      #access_log logs/host.access.log main;
       
      location / {
          root   html;
          index index.html index.htm;
      }

      #error_page 404             /404.html;

      # redirect server error pages to the static page /50x.html
      #
      error_page   500 502 503 504 /50x.html;
      location = /50x.html {
          root   html;
      }

      # proxy the PHP scripts to Apache listening on 127.0.0.1:80
      #
      #location ~ \.php$ {
      #   proxy_pass   http://127.0.0.1;
      #}

      # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
      #
      #location ~ \.php$ {
      #   root           html;
      #   fastcgi_pass   127.0.0.1:9000;
      #   fastcgi_index index.php;
      #   fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
      #   include       fastcgi_params;
      #}

      # deny access to .htaccess files, if Apache's document root
      # concurs with nginx's one
      #
      #location ~ /\.ht {
      #   deny all;
      #}
  }


  # another virtual host using mix of IP-, name-, and port-based configuration
  #
  #server {
  #   listen       8000;
  #   listen       somename:8080;
  #   server_name somename alias another.alias;

  #   location / {
  #       root   html;
  #       index index.html index.htm;
  #   }
  #}


  # HTTPS server
  #
  #server {
  #   listen       443 ssl;
  #   server_name localhost;

  #   ssl_certificate     cert.pem;
  #   ssl_certificate_key cert.key;

  #   ssl_session_cache   shared:SSL:1m;
  #   ssl_session_timeout 5m;

  #   ssl_ciphers HIGH:!aNULL:!MD5;
  #   ssl_prefer_server_ciphers on;

  #   location / {
  #       root   html;
  #       index index.html index.htm;
  #   }
  #}

}

 

 

#http块

#...        
#http块
http {

  include       mime.types; #文件扩展名与文件类型映射表
   
  default_type application/octet-stream; #默认文件类型,默认为text/plain
   
  #access_log off; #取消服务日志
   
  #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 log/access.log myFormat; #combined为日志格式的默认值
   
  sendfile on; #允许sendfile方式传输文件,默认为off,可以在http块,server块,location块
   
  sendfile_max_chunk 100k; #每个进程每次调用传输数量不能大于设定的值,默认为0,即不设上限
   
  keepalive_timeout 65; #连接超时时间,默认为75s,可以在http,server,location块

  upstream mysvr {  
    server 127.0.0.1:7878;
    server 192.168.10.121:3333 backup; #热备
  }
   
   
  error_page 404 https://www.baidu.com; #错误页
   
  #tcp_nopush     on;

  #gzip on;

1、在http模块中有下面的配置,当代理遇到状态码为404时,我们把404页面导向百度。


# 如果我们想让他起作用,我们必须配合着下面的配置一起使用
proxy_intercept_errors on;   # 如果被代理服务器返回的状态码为400或者大于400,设置的error_page配置起作用。默认为off。

 

2、如果我们的代理只允许接受get,post请求方法的一种


proxy_method get;   # 支持客户端的请求方法。post/get;

 

3、设置支持的http协议版本


proxy_http_version 1.0 ; # Nginx服务器提供代理服务的http协议版本1.0,1.1,默认设置为1.0版本

 

4、如果负载均衡中其中web2发生这样的情况,nginx首先会去web1请求,但是nginx在配置不当的情况下会继续分发请求道web2,然后等待web2响应,直到我们的响应时间超时,才会把请求重新分发给web1,这里的响应时间如果过长,用户等待的时间就会越长。

下面的配置是解决方案之一。


proxy_connect_timeout 1;   #nginx服务器与被代理的服务器建立连接的超时时间,默认60秒
proxy_read_timeout 1; #nginx服务器想被代理服务器组发出read请求后,等待响应的超时间,默认为60秒。
proxy_send_timeout 1; #nginx服务器想被代理服务器组发出write请求后,等待响应的超时间,默认为60秒。
proxy_ignore_client_abort on; #客户端断网时,nginx服务器是否终端对被代理服务器的请求。默认为off。

5、如果使用upstream指令配置啦一组服务器作为被代理服务器,服务器中的访问算法遵循配置的负载均衡规则,同时可以使用该指令配置在发生哪些异常情况时,将请求顺次交由下一组服务器处理。


proxy_next_upstream timeout; #反向代理upstream中设置的服务器组,出现故障时,被代理服务器返回的状态值。error|timeout|invalid_header|http_500|http_502|http_503|http_504|http_404|off

error:建立连接或向被代理的服务器发送请求或读取响应信息时服务器发生错误。

timeout:建立连接,想被代理服务器发送请求或读取响应信息时服务器发生超时。

invalid_header:被代理服务器返回的响应头异常。

off:无法将请求分发给被代理的服务器。

http_400,....:被代理服务器返回的状态码为400,500,502,等。

6、如果你想通过http获取客户的真是ip而不是获取代理服务器的ip地址,那么要做如下的设置。


proxy_set_header Host $host; #只要用户在浏览器中访问的域名绑定了 VIP VIP 下面有RS;则就用$host ;host是访问URL中的域名和端口 www.taobao.com:80
proxy_set_header X-Real-IP $remote_addr; #把源IP 【$remote_addr,建立HTTP连接header里面的信息】赋值给X-Real-IP;这样在代码中 $X-Real-IP来获取 源IP
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;#在nginx 作为代理服务器时,设置的IP列表,会把经过的机器ip,代理机器ip都记录下来,用 【,】隔开;代码中用 echo $x-forwarded-for |awk -F, '{print $1}' 来作为源IP

7、下面是关于代理配置的配置文件部分,仅供参考。


  include       mime.types;   #文件扩展名与文件类型映射表
  default_type application/octet-stream; #默认文件类型,默认为text/plain
  #access_log off; #取消服务日志    
  log_format myFormat ' $remote_addr–$remote_user [$time_local] $request $status $body_bytes_sent $http_referer $http_user_agent $http_x_forwarded_for'; #自定义格式
  access_log log/access.log myFormat; #combined为日志格式的默认值
  sendfile on;   #允许sendfile方式传输文件,默认为off,可以在http块,server块,location块。
  sendfile_max_chunk 100k; #每个进程每次调用传输数量不能大于设定的值,默认为0,即不设上限。
  keepalive_timeout 65; #连接超时时间,默认为75s,可以在http,server,location块。
  proxy_connect_timeout 1;   #nginx服务器与被代理的服务器建立连接的超时时间,默认60秒
  proxy_read_timeout 1; #nginx服务器想被代理服务器组发出read请求后,等待响应的超时间,默认为60秒。
  proxy_send_timeout 1; #nginx服务器想被代理服务器组发出write请求后,等待响应的超时间,默认为60秒。
  proxy_http_version 1.0 ; #Nginx服务器提供代理服务的http协议版本1.0,1.1,默认设置为1.0版本。
  #proxy_method get;   #支持客户端的请求方法。post/get;
  proxy_ignore_client_abort on; #客户端断网时,nginx服务器是否终端对被代理服务器的请求。默认为off。
  proxy_ignore_headers "Expires" "Set-Cookie"; #Nginx服务器不处理设置的http相应投中的头域,这里空格隔开可以设置多个。
  proxy_intercept_errors on;   #如果被代理服务器返回的状态码为400或者大于400,设置的error_page配置起作用。默认为off。
  proxy_headers_hash_max_size 1024; #存放http报文头的哈希表容量上限,默认为512个字符。
  proxy_headers_hash_bucket_size 128; #nginx服务器申请存放http报文头的哈希表容量大小。默认为64个字符。
  proxy_next_upstream timeout; #反向代理upstream中设置的服务器组,出现故障时,被代理服务器返回的状态值。error|timeout|invalid_header|http_500|http_502|http_503|http_504|http_404|off
  #proxy_ssl_session_reuse on; 默认为on,如果我们在错误日志中发现“SSL3_GET_FINSHED:digest check failed”的情况时,可以将该指令设置为off。

 

Nginx负载均衡详解

上一篇中我说啦nginx有哪些中负载均衡算法。这一结我就给如果操作配置的给大家做详细说明下。

首先给大家说下upstream这个配置的,这个配置是写一组被代理的服务器地址,然后配置负载均衡的算法。这里的被代理服务器地址有2中写法。

 


upstream mysvr {
    server 192.168.10.121:3333;
    server 192.168.10.122:3333;
  }
server {
      ....
      location ~*^.+$ {        
          proxy_pass http://mysvr; #请求转向mysvr 定义的服务器列表        
      }

 


upstream mysvr {
    server http://192.168.10.121:3333;
    server http://192.168.10.122:3333;
  }
server {
      ....
      location ~*^.+$ {        
          proxy_pass mysvr; #请求转向mysvr 定义的服务器列表        
      }

 

然后,就来点实战的东西。

1、热备:如果你有2台服务器,当一台服务器发生事故时,才启用第二台服务器给提供服务。服务器处理请求的顺序:AAAAAA突然A挂啦,BBBBBBBBBBBBBB.....


upstream mysvr {
    server 127.0.0.1:7878;
    server 192.168.10.121:3333 backup; #热备    
  }

2、轮询:nginx默认就是轮询其权重都默认为1,服务器处理请求的顺序:ABABABABAB....


upstream mysvr {
    server 127.0.0.1:7878;
    server 192.168.10.121:3333;      
  }

3、加权轮询:跟据配置的权重的大小而分发给不同服务器不同数量的请求。如果不设置,则默认为1。下面服务器的请求顺序为:ABBABBABBABBABB....


upstream mysvr {
    server 127.0.0.1:7878 weight=1;
    server 192.168.10.121:3333 weight=2;
}

4、ip_hash:nginx会让相同的客户端ip请求相同的服务器。


upstream mysvr {
    server 127.0.0.1:7878;
    server 192.168.10.121:3333;
    ip_hash;
  }

5、如果你对上面4种均衡算法不是很理解,那么麻烦您去看下我上一篇配的图片,可能会更加容易理解点。

到这里你是不是感觉nginx的负载均衡配置特别简单与强大,那么还没完,咱们继续哈,这里扯下蛋。

关于nginx负载均衡配置的几个状态参数讲解。

  • down,表示当前的server暂时不参与负载均衡。

  • backup,预留的备份机器。当其他所有的非backup机器出现故障或者忙的时候,才会请求backup机器,因此这台机器的压力最轻。

  • max_fails,允许请求失败的次数,默认为1。当超过最大次数时,返回proxy_next_upstream 模块定义的错误。

  • fail_timeout,在经历了max_fails次失败后,暂停服务的时间。max_fails可以和fail_timeout一起使用。


upstream mysvr {
    server 127.0.0.1:7878 weight=2 max_fails=2 fail_timeout=2;
    server 192.168.10.121:3333 weight=1 max_fails=2 fail_timeout=1;    
  }

 

posted @   usersuansuan  阅读(484)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 因为Apifox不支持离线,我果断选择了Apipost!
· 通过 API 将Deepseek响应流式内容输出到前端
点击右上角即可分享
微信分享提示