美文网首页
Nginx-8 动静分离(架构2)

Nginx-8 动静分离(架构2)

作者: Habit_1027 | 来源:发表于2020-01-11 17:53 被阅读0次

环境

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

image.png

一.在静态服务器上查看

[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|jpg|gif|css|js)$ {
        root /opt/;
    }

    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

配置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>

创建php文件 /ding/ 目录下

[root@mpn-salve ding]# vim /ding/index.php 

<?php
echo phpinfo();
?>

配置动态服务器

[root@mpn-salve webapp]# vim /etc/nginx/nginx.conf

user  nginx;
worker_processes  1;

pid        /var/run/nginx.pid;

events {
    worker_connections  1024;
}

http {
    include       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;
    
    server {
        listen       80;
        server_name  localhost;
        
        location ~ \.php$ {
            root   /ding/;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }
    }
}

五. 启动此网站程序

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

六. 配置负载均衡器

# vim /etc/nginx/nginx.conf

user nginx;
worker_processes  2;

worker_rlimit_nofile 102400;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    upstream static {
        server 10.3.134.111;
    }
    upstream python {
        server 10.3.134.99:9090;
    }
    upstream php {
        server 10.3.134.99;
    }
    server {
        listen       80;
        server_name  www.ddd.com;

        location  ~ \.(png|jpg|gif|css|js)$ {
        proxy_pass  http://static;
        }

        location ~ \.php$ {
        proxy_pass http://php;
        }

        location ~ \.php$ {
        proxy_pass http://php;
        }

        location / {
            include uwsgi_params;
            uwsgi_pass python;
        }

    }
}

七. 启动负载均衡器服务

7.1 YUM 安装重启方式

systemctl restart nginx

7.2 编译安装重启方式

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

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

http://10.3.134.72/info

image

相关文章

  • Nginx-8 动静分离(架构2)

    环境 负载均衡器----------------------------------10.3.134.72动态服务...

  • 架构图解

    架构图片理解 单个服务器 单机 动静分离动静分离的架构 反向代理反向代理的架构 负载均衡 缓存相关 业务拆分

  • 企业实战Nginx+Tomcat动静分离架构

    企业实战Nginx+Tomcat动静分离架构 Nginx动静分离简单来说就是把动态跟静态请求分开,不能理解成只是单...

  • Day44-Nginx集群架构:Tomcat动静分离+Rewri

    本章课程内容tomcat动静分离: 1.什么是动静分离? 2.为什么要做动静分离? 3.如何实现动静分离? 4.单...

  • 动静分离架构模式

    上一篇 <<<压缩静态资源减少带宽传输的方式[https://www.jianshu.com/p/49cc17df...

  • 企业级你所要懂的实战应用,Nginx动静分离实战问题详解

    知识要点: Nginx动静分离简介 正则表达式回顾 Nginx动静分离配置 Nginx动静分离简介 动静分离是指在...

  • 动静分离架构,究竟是啥?

    前两天简单介绍了“前台与后台分离”的架构设计准则,又有水友提问:能不能顺带介绍下“动静分离”的架构设计准则呢?今天...

  • 动静分离

    1. 动静分离的实现思路 动静分离是将网站静态资源(HTML,JavaScript,CSS,img等文件)与后台应...

  • 动静分离

    1.什么是动静分离 将动态请求和静态请求区分访问 2.为什么要做动静分离 tomcat本身处理静态效率不高,还会带...

  • 动静分离

    一、动静不分离image.png 1、配置uwsgi image.png修改文件image.png 2、启动应用程...

网友评论

      本文标题:Nginx-8 动静分离(架构2)

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