Nginx 全模块安装及匹配方式、反向代理和负载均衡配置

一、安装 OpenResty

OpenResty® 是一个基于 Nginx 与 Lua 的高性能 Web 平台,其内部集成了大量精良的 Lua 库、第三方模块以及大多数的依赖项。用于方便地搭建能够处理超高并发、扩展性极高的动态 Web 应用、Web 服务和动态网关。
官网://openresty.org/cn/

[[email protected] ~]# wget //openresty.org/package/centos/openresty.repo
[[email protected] ~]# sudo mv openresty.repo /etc/yum.repos.d/
[[email protected] ~]# sudo yum check-update
[[email protected] ~]# sudo yum install -y openresty

管理 OpenResty

[[email protected] ~]# systemctl start openresty.service
[[email protected] ~]# systemctl enable openresty.service

配置文件路径

[[email protected] nginx]# pwd
/usr/local/openresty/nginx

也可以像 nginx 一样管理

[[email protected] sbin]# /usr/local/openresty/nginx/sbin/nginx -s reload
[[email protected] sbin]# /usr/local/openresty/nginx/sbin/nginx -s stop
[[email protected] sbin]# /usr/local/openresty/nginx/sbin/nginx -t

二、匹配方式

2.1 通用匹配

...
   server {
        listen       80;
        server_name  localhost;
        default_type text/html; # 以html标签格式直接返回信息。
    
        # 通用匹配优先级最低。
        location / {
                echo "hello nginx";
        }
    }

测试结果

image-20211207215548161

2.2 完全匹配

完全匹配是优先级别最高的匹配,如果满足则直接匹配。

        location = /a {
                echo "=/a";
        }

测试结果

image-20211207220723390

2.3 进行前缀匹配

# 第二高优先级匹配
        location ^~ /a {
            echo "^~ /a";
        }

测试结果

image-20211207220509009

2.4 正则表达式匹配

# 匹配优先级第三
        location ~ ^/\w {
            echo "~ ^/\w";
        }

测试结果

[[email protected] ~]# curl //localhost/a
=/a
[[email protected] ~]# curl //localhost/b
~ ^/\w

2.5 按照匹配程度

同优先级的,按照匹配程度较高的先匹配

        location ^~ /a {
            echo "^~ /a";
        }
        location ^~ /a/b {
            echo "^~ /a/b";
        }

测试结果

[[email protected] ~]# curl //localhost/a/b
^~ /a/b
[[email protected] ~]# curl //localhost/a/c
^~ /a
[[email protected] ~]# curl //localhost/a/b/c
^~ /a/b

匹配程度一样的,则写在前面的先匹配

        location ~ ^/\w {
            echo "~ ^/\w";
        }
        location ~ ^/[a-z] {
            echo "~ ^/[a-z]";
        }
[[email protected] ~]# curl //localhost/g
~ ^/\w

调换位置之后

        location ~ ^/[a-z] {
            echo "~ ^/[a-z]";
        }
        location ~ ^/\w {
            echo "~ ^/\w";
        }

测试结果

[[email protected] ~]# curl //localhost/g
~ ^/[a-z]

三、反向代理

3.1 常用代理

代理追加虚拟路径,如果是代理到根目录,需要加上“/”,否则找不到页面。

        location /a/ {
            proxy_pass //192.168.115.21:8081/;
        }

测试结果

image-20211207223247113

多个代理

        location /a/ {
            proxy_pass //192.168.115.21:8081/;
        }
        location /b/ {
            proxy_pass //192.168.115.21:8080/;
        }

测试结果

image-20211207224013825

3.2 根据不同的域名代理

    server
    {
        listen 80;
        server_name test.xxxxx.net;
        location / {
            proxy_redirect off;
            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_pass //192.168.3.222;
        }
    }
    server
    {
        listen 80;
        server_name wiki.xxxxx.net;
        location / {
            proxy_redirect off;
            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_pass //127.0.0.1:8181;
        }
    }

测试结果

image-20220106160606993

四、负载均衡

4.1 随机负载

    upstream group1 {
        server 192.168.115.21:8080;
        server 192.168.115.21:8081;
    }
        location /a/ {
            proxy_pass //group1/;
        }

4.2 配置权重

    upstream group1 {
        server 192.168.115.21:8080 weight=1;
        server 192.168.115.21:8081 weight=1;
    }

        location /a/ {
            proxy_pass //group1/;
        }
Tags: