Nginx-7 动静分离(架构1)

环境

负载均衡器----------------------------------10.3.134.72
动态服务器----------------------------------10.3.134.99
静态服务器(yum安装的nginx)----------10.3.134.111

image.png

一. 拷贝相关的静态资源到 静态服务器的对应目录

把动态服务器里面的图片拷贝到静态服务器/opt/目录下

 

[root@mpn-salve webapp]# scp king.jpg qf.png qf.css root@10.3.134.111:/opt/
root@10.3.134.111's password: 
king.jpg                                             100%  232KB  24.5MB/s   00:00    
qf.png                                               100% 9563     2.5MB/s   00:00    
qf.css                                               100%  800   434.4KB/s   00:00    

在静态服务器上查看

 

[root@yum-n opt]# ls
king.jpg  qf.css  qf.png

二. 配置静态服务器

备份原来的配置文件,之后创建如下两个新的文件,没有的目录请自行创建。

 

user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;

events {
    worker_connections  1024;
}

http {
    include       /etc/nginx/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  /var/log/nginx/access.log  main;
    sendfile        on;
    keepalive_timeout  65;
    include /etc/nginx/conf.d/*.conf;
}

 

server {
    listen       80;
    server_name  localhost;

    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;

    # 请求的静态资源到本地的 /opt/ 目录下获取
    location ~ \.(png|css|jpg|js)$ {
        root /opt/;
    }

    # 其他请求转发到 10.3.134.99:9090 动态资源服务器
    location / {
           include uwsgi_params;
           uwsgi_pass 10.3.134.99:9090;
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
}

重启Nginx服务

YUM 安装重启方式

 

systemctl restart nginx

编译安装重启方式

 

nginx -s stop  # 先停止服务
nginx          # 再启动服务

三. 修改动态网站服务器程序代码

image.png

配置wsgi.py

 

[root@mpn-salve webapp]# vim wsgi.py 

headers = {
   'text': ('Content-Type', 'text/plain;charset=utf-8'),
   'html': ('Content-Type', 'text/html;charset=utf-8'),
   'json': ('Content-Type', 'application/json;charset=utf-8'),
   'js': ('Content-Type', 'application/javascript'),
   'css': ('Content-Type', 'text/css'),
   'jpg': ('Content-Type', 'application/x-jpg'),
   'png': ('Content-Type', 'image/png'),
}

import views

def application(env, start_response):
    if env['PATH_INFO'] == '/':
        start_response('200 OK', [headers['text']])
        content = views.qf_index()

        return [b'hello']
    elif env['PATH_INFO'] == '/api/json':
        start_response('200 OK', [headers['json']])
        content = views.qf_json()
        print("==>",content)
        return [content]

    elif env['PATH_INFO'] == '/info':
        start_response('200 OK', [headers['html']])
        content = views.info()
        return [content]

配置qf-uwsgi.ini

 

[root@mpn-salve webapp]# cat qf-uwsgi.ini 
[uwsgi]
socket = 0.0.0.0:9090
chdir = /opt/webapp/
wsgi-file = wsgi.py
processes = 4
threads = 2
stats = 127.0.0.1:9191

配置views.py

 

[root@mpn-salve webapp]# cat views.py 
import json

def qf_index():
    return bytes("hello", encoding='utf-8')

def qf_json():
    data = {"name": "shark", "age": 18}
    return bytes(json.dumps(data), encoding='utf-8')

def info():
    with open('info.html', 'rb') as f:
        html = f.read()
    return html

def qf_logo():
    with open('qf.png', 'rb') as f:
        img = f.read()
    return img

def king():
    with open('king.jpg', 'rb') as f:
        img = f.read()
    return img

def handle_css():
    with open('qf.css', 'rb') as f:
        css = f.read()
    return css

配置info.html

 

[root@mpn-salve webapp]# vim info.html 

<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- 上述3个meta标签*必须*放在最前面,任何其他内容都*必须*跟随其后! -->
    <title>测试一下</title>

     <link rel="stylesheet" type="text/css" href="/qf.css">

  </head>
  <body>
    <h1>走一走</h1>
    <img src="/king.jpg" alt="金鼠送福" class="img-rounded">

    <h2 class="bg-danger">看一看</h2>
    <table class="table ">
      <thead>
        <tr>
          <th>序号</th>
          <th>姓名</th>
          <th>年龄</th>

        </tr>
      </thead>
      <tbody>
        <tr>
          <th scope="row">1</th>
          <td>{name}</td>
          <td>{age}</td>
        </tr>
      </tbody>
    </table>

  </body>
</html>

五. 启动此网站程序

 

[root@mpn-salve webapp]# uwsgi qf-uwsgi.ini 

六. 配置负载均衡器

 

/ # cat /etc/nginx/nginx.conf

user  nginx;
worker_processes  auto;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/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  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
}

 

/ # cat /etc/nginx/conf.d/default.conf
upstream myweb {
   server 10.3.134.111;
}

server {
    listen       80;
    server_name  localhost;

    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;

    location / {
           proxy_pass http://myweb;
    }
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

}

七. 启动负载均衡器服务

7.1 YUM 安装重启方式

 

systemctl restart nginx

7.2 编译安装重启方式

 

nginx -s stop  # 先停止服务
nginx          # 再启动服务

八. 浏览器访问负载均衡器的地址

 

http://10.3.134.72/info



 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值