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

环境

负载均衡器----------------------------------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
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,651评论 6 501
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,468评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 162,931评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,218评论 1 292
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,234评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,198评论 1 299
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,084评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,926评论 0 274
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,341评论 1 311
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,563评论 2 333
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,731评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,430评论 5 343
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,036评论 3 326
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,676评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,829评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,743评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,629评论 2 354

推荐阅读更多精彩内容