Nginx服务实现动静分离

目录

1 Nginx实现动静分离

1.1 http_proxy_module

Proxy_pass指令属于ngx_http_proxy_module模块,此模块可以将请求转发到另一台服务器,在实际的反向代理工作中,会通过location功能匹配指定的URI,然后把接收到的服务匹配URI的请求通过proxy_pass抛给定义好的upstream节点池。

官方地址:

http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_pass

http proxy模块相关参数说明
proxy_set_header设置http请求header项传给后端服务器节点,例如:可实现让代理后端的服务器节点获取访问客户端用户的真实IP地址
client_body_buffer_size用于指定客户端请求主体缓冲区大小
proxy_connect_timeout表示反向代理与后端节点服务器连接的超时时间,即发起握手等候响应的超时时间
proxy_send_timeout表示代理后端服务器的数据回传时间,即在规定时间之内后端服务器必须传完所有的数据,否则,Nginx将断开这个连接
proxy_read_timeout设置Nginx从代理的后端服务器获取信息的时间,表示连接建立成功后,Nginx等待后端服务器的响应时间,其实是Nginx已经进入后端的排队之中等候处理的时间
proxy_buffer_size设置缓冲区大小,默认该缓冲区大小等于指令proxy_buffers设置的大小
proxy_buffers设置缓冲区的数量和大小。nginx从代理的后端服务器获取的响应信息,会放置到缓冲区
proxy_busy_buffers_size用于设置系统很忙时可以使用的proxy_buffers大小,官方推荐的大小为proxy_buffers*2
proxy_temp_file_write_size指定proxy缓存临时文件的大小

1.2 动静分离原理及环境准备

通过Nginx实现动静分离,即通过Nginx反向代理配置规则实现让动态资源和静态资源及其他业务分别由不同的服务器解析,以解决网站性能、安全、用户体验等重要问题。

这里写图片描述

环境准备

HostnameIP说明
lb0110.0.0.5Nginx主负载均衡器
lb0210.0.0.6Nginx辅负载均衡器
web0110.0.0.8web01服务器
web0210.0.0.7web02服务器

VIP(虚拟IP):10.0.0.3

若进行以下测试,需要准备好Nginx环境

LNMP之Nginx服务搭建及三种类型虚拟主机 
keepalived+nginx反向代理负载均衡配置

1.3 详细配置

# 写一个proxy的配置文件,然后可以直接在nginx.conf中include调用
[root@lb01 ~]# vim /application/nginx/conf/proxy.conf
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_connect_timeout 60;
proxy_send_timeout 60;
proxy_read_timeout 60;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;

# 修改配置文件如下,可直接复制
[root@lb01 ~]# vim /application/nginx/conf/nginx.conf
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;

    upstream static_pools{
        server 10.0.0.7:80 weight=1;
    }
    upstream upload_pools{
        server 10.0.0.7:8080 weight=1;
    }
    upstream default_pools{
        server 10.0.0.8:80 weight=1;
    }

    server {
        listen 80;
        server_name www.rsq.com;
        location / {
            proxy_pass http://default_pools;
            include proxy.conf;
        }
        location /static/ {
            proxy_pass http://static_pools;
            include proxy.conf;
        }
        location /upload/ {
            proxy_pass http://upload_pools;
            include proxy.conf;
        }
    }
}

# 修改web02的www配置文件,使其监听8080端口,修改完记得重载服务
# 这里的www.conf是被调用的配置文件,也可以直接写入到nginx.conf
[root@web02 ~]# cat /application/nginx/conf/extra/www.conf
    server {
        listen       80;
        server_name  www.rsq.com;
        location / {
            root   html/www;
            index  index.html index.htm;
        }
    }

    server {
        listen       8080;
        server_name  www.rsq.com;
        location / {
            root   html/www;
            index  index.html index.htm;
        }
    }

# 创建测试文件
[root@web02 ~]# mkdir /application/nginx/html/www/{static,upload}
[root@web02 ~]# echo "static page" >/application/nginx/html/www/static/index.html
[root@web02 ~]# echo "upload page" >/application/nginx/html/www/upload/index.html
[root@web01 ~]# cat /application/nginx/html/www/index.html
web01_www
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80

1.3 访问测试

测试访问之前需要在Client中做hosts解析

# 这里的10.0.0.3为keepalived生成的虚拟IP(VIP)
cat >>/etc/hosts<<EOF
10.0.0.3 www.rsq.com bbs.rsq.com blog.rsq.com
EOF
  • 1
  • 2
  • 3
  • 4

这里写图片描述

2 根据客户端的不同进行转发

2.1 基本配置

# 保持以上环境,可把配置文件备份并进行新测试
# 修改配置文件如下,可直接复制
[root@lb01 ~]# vim /application/nginx/conf/nginx.conf
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;

    upstream android_pools {
        server 10.0.0.7:80 weight=1;
    }
    upstream iphone_pools {
        server 10.0.0.7:8080 weight=1;
    }
    upstream pc_pools {
        server 10.0.0.8:80 weight=1;
    }

    server {
        listen 80;
        server_name www.rsq.com;
        location / {
            if ($http_user_agent ~* "android")
            {
                proxy_pass http://android_pools;
            }
            if ($http_user_agent ~* "iphone")
            {
                proxy_pass http://iphone_pools;
            }
                proxy_pass http://pc_pools;
                include proxy.conf
        }
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40

2.2 测试访问

此次测试由于环境问题,没有测试,大家可以在Windows VMware workstation上做NAT映射,或者电脑开热点使手机连接,虚拟机网卡选择桥接模式上网,保证手机可以访问到虚拟机web服务。测试流程和动静分离测试保持类似。

# curl -A 可自定义User-Agent,实例如下
[root@lb01 ~]# curl -A android www.rsq.com
[root@lb01 ~]# curl -A iphone www.rsq.com

转载至https://blog.csdn.net/mr_rsq/article/details/80386697#11-httpproxymodule


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值