Liunx之nginx代理

一、代理

正向代理#

正向代理,也就是传说中的代理, 他的工作原理就像一个跳板(VPN),简单的说:

我是一个用户,我访问不了某网站,但是我能访问一个代理服务器,这个代理服务器呢,他能访问那个我不能访问的网站,于是我先连上代理服务器,告诉他我需要那个无法访问网站的内容,代理服务器去取回来,然后返回给我。

反向代理#

对于客户端而言,代理服务器就像是原始服务器。

实现一个反向代理#

同过一个中间服务器来接受请求,并去实际的服务器中取数据,文件,返回。

准备两个服务器,这里我准备的两个虚拟机服务器。

master 192.168.11.61  # 主负载,充当中介,反向代理 slave 192.168.11.122  # web1 服务器,提供资源

主负载均衡节点的配置文件#

主负载的 nginx 是通过 yum 直接安装的,修改默认的配置文件 /etc/nginx/nginx.conf

 
#user nobody; # ngin 进程所使用的用户 worker_processes 1; # nginx 运行的 work 进程数量,建议与 cpu 数量一致或 auto ​ #error_log logs/error.log; # nginx 错误日志,在安装路径下的 logs 文件家中 #error_log logs/error.log notice; #error_log logs/error.log info; #error_log "pipe:rollback logs/error_log interval=1d baknum=7 maxsize=2G"; ​ #pid logs/nginx.pid; # nginx 服务运行后产生的 pid 进程号 ​ # events {worker_connections 1024; # 每个 worker 进程支持的最大连接数 # user epool; # 事件驱动模型,epool 默认} ​ # 公共的配置定义在 http{} 中 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; keepalive_timeout 65; # nginx 的虚拟主机参数,网站的功能性参数定义 # 每一个 server 代表一个网站 server {listen 80; # 指定端口 server_name localhost; # 指定域名 ​ # 控制 url 路径访问 location / { # 开启 nginx 反向代理一台服务器 proxy_pass http://192.168.16.122; root html; # 存放网站静态页面的路径 index index.html index.htm; # 存放服务器返回的 index 页面文件} error_page 500 502 503 504 /50x.html; # 配置的 50x 错误页面 location = /50x.html {root html;} } }
 

web1 服务器是我们的主要数据提供服务器,也就是我们通过 yum 安装的 tengine,提供了访问请求的页面数据展示。

这里我的具体的配置前面博客写过,在 /opt/tengine231/conf/nginx.conf 中里面。web1 的 ip 地址是 192.168.16.122.

# 检查语法并启动 nginx /opt/tengine/sbin/nginx -t /opt/tengine/sbin/nginx

配置好了后可以通过访问 192.168.16.61 代理服务器,代理会把请求转发给服务器 192.168.122,获取页面返回展示。

二、location 语法详解

1、location 语法的优先级#

匹配符 匹配规则 优先级
=    精确匹配    1
^~    以某个字符串开头    2
~    区分大小写的正则匹配    3
~*    不区分大小写的正则匹配    4
!~    区分大小写不匹配的正则    5
!~*    不区分大小写不匹配的正则    6
/    通用匹配,任何请求都会匹配到    7

2、nginx.conf 配置文件示例#

server {
    listen 80;
    server_name pythonav.cn;
​
    #优先级 1, 精确匹配,根路径
    location =/ {
        return 400;
    }
​
    #优先级 2, 以某个字符串开头, 以 av 开头的,优先匹配这里,区分大小写
    location ^~ /av {
       root /data/av/;
    }
​
    #优先级 3,区分大小写的正则匹配,匹配 /media***** 路径
    location ~ /media {
          alias /data/static/;
    }
​
    #优先级 4 ,不区分大小写的正则匹配,所有的 ****.jpg|gif|png 都走这里
    location ~* .*\.(jpg|gif|png|js|css)$ {
       root  /data/av/;
        }
​
    #优先 7,通用匹配
    location / {
        return 403;
    }
}

3、root 和 alias 区别#

root 与 alias 主要区别在于 nginx 如何解释 location 后面的 url,这会使两者分别以不同的方式将请求映射到服务器文件上。

  • root 的处理结果:root 路径 +location 路径

  • alias 的处理结果:使用 alias 路径替换 location 路径

alias 是一个目录别名的定义,root 则是最上层目录的定义。还有就是 alias 后面必须要用 "/" 结束,否则找不到文件,而 root 则可有可无。

root 方法#

语法  root  路径;
默认值 root   html;
配置块  http{}   server {}   location{}

alias 方法#

语法: alias  路径
配置块  location{}

root 示例#

root 实例 1:
location ^~ /av {
    root /data/av;   注意这里可有可无结尾的   /
    }
# 请求 url 是 pythonav.cn/av/index.html 时
web 服务器会返回服务器上的 /data/av/av/index.html
​
root 实例 2:
location ~* .*\.(jpg|gif|png|js|css)$ {
       root  /data/av/;
}
# 请求 url 是 pythonav.cn/girl.gif 时
web 服务器会返回服务器上的 /data/static/girl.gif

alias 示例#

alias 参数是使用 alias 路径替换 location 路径
alias 是一个目录的别名
注意 alias 必须有 "/"  结束!
alias 只能位于 location 块中
​
请求 url 是 pythonav.cn/av/index.html 时
web 服务器会返回服务器上的/data/static/index.html
​
location ^~ /av {
    alias /data/static/;
}

三、nginx 负载均衡

概述:

Web 服务器,直接面向用户,往往要承载大量并发请求,单台服务器难以负荷,我使用多台 WEB 服务器组成集群,前端使用 Nginx 负载均衡,将请求分散的打到我们的后端服务器集群中,
实现负载的分发。那么会大大提升系统的吞吐率、请求性能、高容灾

Nginx 要实现负载均衡需要用到 proxy_pass 代理模块配置

Nginx 负载均衡与 Nginx 代理不同地方在于

Nginx 代理仅代理一台服务器,而 Nginx 负载均衡则是将客户端请求代理转发至一组 upstream 虚拟服务池

Nginx 可以配置代理多台服务器,当一台服务器宕机之后,仍能保持系统可用。

upstream 配置#

在 nginx.conf > http 区域中

upstream django {
       server 10.0.0.10:8000;
       server 10.0.0.11:9000;
}

在 nginx.conf > http 区域 >  server 区域  > location 配置中

添加 proxy_pass

location / {
            root   html;
            index  index.html index.htm;
            proxy_pass http://django;
}

此时初步负载均衡已经完成,upstream 默认按照轮训方式负载,每个请求按时间顺序逐一分配到后端节点。

upstream 分配策略#

weight 权重

upstream django {
       server 10.0.0.10:8000 weight=5;
       server 10.0.0.11:9000 weight=10;#这个节点访问比率是大于 8000 的
}

ip_hash

每个请求按访问 ip 的 hash 结果分配,这样每个访客固定访问一个后端服务器
upstream django {
    ip_hash;
       server 10.0.0.10:8000;
       server 10.0.0.11:9000;
}

backup

在非 backup 机器繁忙或者宕机时,请求 backup 机器,因此机器默认压力最小

upstream django {
       server 10.0.0.10:8000 weight=5;
       server 10.0.0.11:9000;
       server node.oldboy.com:8080 backup;
}

负载均衡实验环境规划#

角色            ip                    主机名
lb01        192.168.119.10        lb01    
web01        192.168.119.11        web01
web02        192.168.119.12        web02

关闭防火墙#

iptables -F
sed  -i 's/enforcing/disabled/' /etc/selinux/config

systemctl stop firewalld
systemctl disable firewalld

web01 服务器配置 nginx,创建 index.html#

server {
        listen       80;
        server_name  192.168.119.11;
        location / {
        root /node;
            index  index.html index.htm;
        }
}

mkdir /node
echo
'i am web01' > /node/index.html

#启动 NGINX
./sbgin/nginx

web02 服务器配置 nginx,创建 index.html#

server {
    listen       80;
    server_name  192.168.119.12;
    location / {
        root /node;
        index  index.html index.htm;
}

mkdir /node
echo
'i am web02...' > /node/index.html
#启动 nginx
./sbing/nginx

 

配置 lb01 服务器的 nginx 负载均衡#

1. 检查 lb01 的 nginx.conf

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    upstream node {
      server 192.168.119.11:80;
      server 192.168.119.12:80;
}
    server {
        listen       80;
        server_name 192.168.119.10;
        location / {
          proxy_pass http://node;
          include proxy_params;  #需要手动创建
        }
    }
}

2. 手动创建 proxy_params 文件,文件中存放代理的请求头相关参数

[root@lb01 conf]# cat /opt/nginx/conf/proxy_params
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

proxy_connect_timeout 30;
proxy_send_timeout
60;
proxy_read_timeout
60;

proxy_buffering on;
proxy_buffer_size 32k;
proxy_buffers 4 128k;

启动 lb01 负载均衡 nginx 服务

./sbin/nginx

访问 lb01 节点 nginx,反复刷新

负载均衡调度算法#

调度算法      概述
轮询        按时间顺序逐一分配到不同的后端服务器 (默认)
weight       加权轮询,weight 值越大, 分配到的访问几率越高
ip_hash      每个请求按访问 IP 的 hash 结果分配, 这样来自同一 IP 的固定访问一个后端服务器
url_hash      按照访问 URL 的 hash 结果来分配请求, 是每个 URL 定向到同一个后端服务器
least_conn    最少链接数, 那个机器链接数少就分发

1. 轮询 (不做配置,默认轮询)

2.weight 权重 (优先级)

3.ip_hash 配置,根据客户端 ip 哈希分配,不能和 weight 一起用