zoukankan      html  css  js  c++  java
  • nginx*实现后端web的读写分离

    1.环境

    角色 ip 主机名
    负载均衡节点 10.0.0.11 nginx-lb01
    可读写web01节点 10.0.0.12 nginx-web01
    只读web02节点 10.0.0.13 nginx-web02

    2.nginx-lb01的nginx配置文件如下

    [root@nginx-lb01 ~]# cat /etc/nginx/nginx.conf
    
    worker_processes 1;
    
    events {
    
    worker_connections 1024;
    
    }
    
    http {
    
    include mime.types;
    
    default_type application/octet-stream;
    
    sendfile on;
    
    keepalive_timeout 65;
    
    upstream backend1 {
    
    server 10.0.0.12;
    
    }
    
    upstream backend2 {
    
    server 10.0.0.13;
    
    }
    
    server {
    
    listen 80;
    
    server_name localhost;
    
    location / {
    
    if ($request_method = POST ) {
    
    proxy_pass http://backend1;
    
    }
    
    proxy_pass http://backend2;
    
    }
    
    }
    
    }
    

    3.nginx-web01和nginx-web02配置文件相同

    worker_processes 1;
    
    events {
    
    worker_connections 1024;
    
    }
    
    http {
    
    include mime.types;
    
    default_type application/octet-stream;
    
    sendfile on;
    
    keepalive_timeout 65;
    
    server {
    
    listen 80;
    
    server_name localhost;
    
    root html;
    
    index index.php index.html index.htm;
    
    location / {
    
    }
    
    location ~ .php$ {
    
    fastcgi_pass 127.0.0.1:9000;
    
    fastcgi_index index.php;
    
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    
    include fastcgi_params;
    
    }
    
    }
    
    }
    

    nginx-web01和nginx-web02区别只有nfs挂载的区别

    nginx-web01的挂载参数为

    mount -t nfs 10.0.0.11:/data wp-content/uploads

    nginx-web02的挂载参数为

    mount -t nfs -o ro 10.0.0.11:/data wp-content/uploads

    上传图片测试,抓包结果如下

    从上图可以看出,上传图片之后,先是post到负载均衡——> post到web01——>返回结果给负载均衡——>返回结果给用户

    接着又发起了一个新的请求,这次是get请求,先get到负载均衡——>get到web02——>返回结果给负载均衡——>返回结果给用户

    到这里,已经实现了POST请求方法走web01,GET请求方法走web02

  • 相关阅读:
    url 百分号解密
    16.UA池和代理池
    15.scrapy框架之日志等级、请求传参、提高scrapy框架的爬取效率
    14. scrip框架之5大核心组件和post请求
    13.scrapy 框架之递归解析(手动发送请求),
    12. scrapy 框架持续化存储
    11.scrapy框架简介和基础应用
    10. 移动端数据爬取
    09.python之网络爬虫之selenium、phantomJs和谷歌无头浏览器的自动化操作
    08 python之网络爬虫之乱码问题
  • 原文地址:https://www.cnblogs.com/Forever-x/p/10862672.html
Copyright © 2011-2022 走看看