美文网首页
A3-Nginx-集群篇

A3-Nginx-集群篇

作者: 鄙人_阿K | 来源:发表于2021-04-23 23:35 被阅读0次

    Nginx-文章总纲

    https://www.jianshu.com/p/07cdd29d2a50

    友情链接

    1、基础篇:ngxin 的简介,安装,目录说明,配置文件详解(理论为主)
    https://www.jianshu.com/p/d46dc6bce6ed
    2、应用篇:静态部署,后端配置,反向代码,负载均衡,缓存集成(实操为主)
    https://www.jianshu.com/p/3a39e18550b5
    3、集群篇:集群搭建,高可用解决方案,制作下载站点和用户认证(提升)
    https://www.jianshu.com/p/2e9ca678ef73
    4、模块篇:ngx-lua的结合使用,lua基本语法介绍(综合)
    https://www.jianshu.com/p/2e9ca678ef73

    一、Nginx反向代理

    1、正向代理案例
    image.png

    (1)编写配置

    http { 
        log_format main 'client send request=>clientIp=$remote_addr serverIp=>$host'; 
        server{ 
            listen 80; 
            server_name localhost; 
            access_log logs/access.log main; 
            location / { 
                root html; 
                index index.html index.htm; 
            } 
        } 
    }
    

    (2)打开日志文件,logs/access.log


    image.png

    (3)代理服务器设置:

    server {
        listen 82; 
        resolver 8.8.8.8; 
        location /{ 
            proxy_pass http://$host$request_uri; 
        } 
    }
    

    (4)设置代理IP


    image.png

    (5)再次访问日志 ip 就不同了,正向代理应用的很少。

    2、Nginx反向代理的配置语法(介绍)

    Nginx反向代理模块的指令是由ngx_http_proxy_module模块进行解析,该模块在安装Nginx的时候已经自己加装到
    Nginx中了,接下来我们把反向代理中的常用指令一一介绍下:

    proxy_pass
    proxy_set_header
    proxy_redirect

    1、proxy_pass

    该指令用来设置被代理服务器地址,可以是主机名称、IP地址加端口号形式。


    image.png

    URL:为要设置的被代理服务器地址,包含传输协议( http , https:// )、主机名称或IP地址加端口号、URI等要素。

    server { 
        listen 80; 
        server_name localhost; 
        location /{ 
            #proxy_pass http://192.168.200.146; 
            proxy_pass http://192.168.200.146/; 
        } 
    }
    #当客户端访问 http://localhost/index.html,效果是一样的
    server{ 
        listen 80; 
        server_name localhost; 
        location /server{ 
            #proxy_pass http://192.168.200.146; 
            proxy_pass http://192.168.200.146/; 
        } 
    }
    #当客户端访问 http://localhost/server/index.html 
    #这个时候,第一个proxy_pass就变成了 http://localhost/server/index.html
    #第二个proxy_pass就变成了http://localhost/index.html效果 就不一样了。
    
    2、proxy_set_header

    该指令可以更改Nginx服务器接收到的客户端请求的请求头信息,然后将新的请求头发送给代理的服务器


    image.png

    需要注意的是,如果想要看到结果,必须在被代理的服务器上来获取添加的头信息。

    被代理服务器: [192.168.200.146]

    server {
        listen 8080; 
        server_name localhost; 
        default_type text/plain; 
        return 200 $http_username; 
    }
    

    代理服务器: [192.168.200.133]

    server {
        listen 8080; 
        server_name localhost; 
        location /server { 
            proxy_pass http://192.168.200.146:8080/; 
            proxy_set_header username TOM; 
        } 
    }
    
    3、proxy_redirect

    该指令是用来重置头信息中的"Location"和"Refresh"的值


    image.png

    服务端[192.168.200.146]:

    server { 
        listen 8081; 
        server_name localhost; 
        if (!-f $request_filename){ 
            return 302 http://192.168.200.146; 
        } 
    }
    

    代理服务端[192.168.200.133]

    
    

    相关文章

      网友评论

          本文标题:A3-Nginx-集群篇

          本文链接:https://www.haomeiwen.com/subject/fgbulltx.html