Ming Ming
首页
  • VUE2
  • VUE3
  • 设计模式
  • JUC
  • Nginx
  • RabbitMQ
  • Redis
  • linux
  • SQL
  • MyBatis
  • 软件使用
  • 工具类
  • 在线工具

    • githubHosts (opens new window)
    • 正则表达式 (opens new window)
    • 文件转换器 (opens new window)
    • 代码在线运行 (opens new window)
GitHub (opens new window)
首页
  • VUE2
  • VUE3
  • 设计模式
  • JUC
  • Nginx
  • RabbitMQ
  • Redis
  • linux
  • SQL
  • MyBatis
  • 软件使用
  • 工具类
  • 在线工具

    • githubHosts (opens new window)
    • 正则表达式 (opens new window)
    • 文件转换器 (opens new window)
    • 代码在线运行 (opens new window)
GitHub (opens new window)
  • 后端

    • designPattern
    • JUC
    • Nginx
      • 一、概念
      • 二、配置
      • 三、高可用
    • RabbitMQ
    • redis
  • 后端
  • 后端
J-Ming
2021-12-3
目录

Nginx

# 一、概念

一个高性能的HTTP和代理web服务器

特点:

  • 占用内存少
  • 并发能力强

作用:

  • 反向代理

    正向代理: 一个位于客户端和原始服务器之间的服务器,为了从原始服务器取得内容,客户端向代理发送一个请求并制定目标(原始服务器),然后代理向原始服务器转发请求并将获得的内容返回给客户端,客户端才能使用正向代理

    ​ 正向代理 ​ A知道C让B去做

    反向代理: 以代理服务器来接受internet上的连接请求,然后将请求转发给内部网络上的服务器,并将从服务器上得到的结果返回给internet上请求的客户端,此时代理服务器对外表现为一个反向代理服务器 反向代理 A不知道C但B却访问C

  • 负载均衡

    1. 轮询(默认) 每个请求按时间顺序逐一分配到不同服务器
    2. weight 权重,默认为1,值越大被分配的客户端越多
    3. ip_hash 每个请求按访问IP的hash结果分配,每个访客固定访问一个服务器
    4. fair 按后端服务器的响应时间来分配,响应时间短的优先分配
    upstream mysvr {   
          server 192.168.10.121:8080;
          server 192.168.10.121:8081;
        }
    server {
            keepalive_requests 120; #单连接请求上限次数。
            listen       4545;   #监听端口
            server_name  127.0.0.1;   #监听地址       
            location  ~*^.+$ {       #请求的url过滤,正则匹配,~为区分大小写,~*为不区分大小写。
               proxy_pass  http://mysvr;  #请求转向mysvr 定义的服务器列表       
            } 
        }
    
  • 动静分离

    server {
        listen       4545;   #监听端口
        server_name  127.0.0.1;   #监听地址       
        location  /www/ {
            root  /data/;
            index index.html index.htm;
    	} 
        location  /www/ {
            root  /data/;
            autoindex on;
        } 
    }
    

# 二、配置

配置文件:

  • 全局块:配置影响nginx全局的指令。一般有运行nginx服务器的用户组,nginx进程pid存放路径,日志存放路径,配置文件引入,允许生成worker process数等。
  • events块:配置影响nginx服务器或与用户的网络连接。有每个进程的最大连接数,选取哪种事件驱动模型处理连接请求,是否允许同时接受多个网路连接,开启多个网络连接序列化等。
  • http块:可以嵌套多个server,配置代理,缓存,日志定义等绝大多数功能和第三方模块的配置。如文件引入,mime-type定义,日志自定义,是否使用sendfile传输文件,连接超时时间,单连接请求数等。
  • server块:配置虚拟主机的相关参数,一个http中可以有多个server。
  • location块:配置请求的路由,以及各种页面的处理情况。
########### 每个指令必须有分号结束#################
#user administrator administrators;  #配置用户或者组,默认为nobody nobody
#worker_processes 2;  #允许生成的进程数,默认为1
#pid /nginx/pid/nginx.pid;   #指定nginx进程运行文件存放地址
error_log log/error.log debug;  #制定日志路径,级别。这个设置可以放入全局块,http块,server块,级别以此为:debug|info|notice|warn|error|crit|alert|emerg
events {
    accept_mutex on;   #设置网路连接序列化,防止惊群现象发生,默认为on
    multi_accept on;  #设置一个进程是否同时接受多个网络连接,默认为off
    #use epoll;      #事件驱动模型,select|poll|kqueue|epoll|resig|/dev/poll|eventport
    worker_connections  1024;    #最大连接数,默认为512
}
http {
    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块。

    upstream mysvr {   
      server 127.0.0.1:7878;
      server 192.168.10.121:3333 backup;  #热备
    }
    error_page 404 https://www.baidu.com; #错误页
    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           
        } 
    }
}
  • $remote_addr 与 $http_x_forwarded_for 用以记录客户端的ip地址;
  • $remote_user :用来记录客户端用户名称;
  • $time_local : 用来记录访问时间与时区;
  • $request : 用来记录请求的url与http协议;
  • $status : 用来记录请求状态;成功是200;
  • $body_bytes_s ent :记录发送给客户端文件主体内容大小;
  • $http_referer :用来记录从那个页面链接访问过来的;
  • $http_user_agent :记录客户端浏览器的相关信息;

# 三、高可用

高可用

需求:

  • 两台Nginx服务器
  • 安装keepalived软件
  • 虚拟IP

配置:

  1. 安装keepalived

    # 安装完成在 /etc 里面生成目录 keepalived,有文件 keepalived.conf
    yum install keepalived --y
    
  2. 修改/etc/keepalived/keepalivec.conf 配置文件

    global_defs {
    	# 指定keepalived在发生切换时需要发送email到的对象,一行一个
        notification_email {
            [email protected]
            [email protected]
            [email protected]
    	}
    	notification_email_from [email protected] #指定发件人
        smtp_server 192.168.17.129 #指定smtp服务器地址
        smtp_connect_timeout 30#指定smtp连接超时时间
        router_id LVS_DEVEL #运行keepalived机器的一个标识 在/ect/hosts下载配置
    }
    # 检测脚本
    vrrp_script chk_http_port { 
        script "/usr/local/src/nginx_check.sh"
        interval 2  #(检测脚本执行的间隔) 
        weight 2    #权重 
    } 
    vrrp_instance VI_1 { 
        state BACKUP   # 备份服务器上将 MASTER 改为 BACKUP 
        interface ens33    # 服务器网卡(ifconfig查看)
        virtual_router_id 51   # 主、备机的 virtual_router_id 必须相同 
        priority 90    # 主、备机取不同的优先级,主机值较大,备份机值较小 
        advert_int 1 
        authentication {
            auth_type PASS
            auth_pass 1111
        }
        # 虚拟ip
        virtual_ipaddress {
        	192.168.17.50 // VRRP H 虚拟地址
        }
    }
    
  3. 在/usr/local/src 添加检测脚本nginx_check.sh

    #!/bin/bash
    A=`ps -C nginx –no-header |wc -l`
    if [ $A -eq 0 ];then
     /usr/local/nginx/sbin/nginx
     sleep 2
     if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then
     killall keepalived
     fi
    fi
    
编辑 (opens new window)
上次更新: 2024/06/12, 02:20:36
JUC
RabbitMQ

← JUC RabbitMQ→

Theme by Vdoing | Copyright © 2024-2024 Evan Xu | MIT License
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式