• nginx动静分离


    nginx 实现动静分离 :
    为了加快网站的解析速度,可以把动态页面和静态页面由不同的服务器来解析,加快解析速度。降低原来单个服务器 的压力。 在动静分离的tomcat的时候比较明显,因为tomcat解析静态很慢,其实这些原理的话都很好理解,简单来 说,就是使用正则表达式匹配过滤,然后交个不同的服务器。
    1、准备环境
    192.168.62.159 代理服务器

    192.168.62.157 动态资源

    192.168.62.155 静态资源
    准备一个nginx代理 两个http 分别处理动态和静态。


    192.168.62.159 代理服务器

    1.配置nginx反向代理upstream;

    [root@nginx-server conf.d]# cat upstream.conf

    upstream static {

    server 192.168.62.155:80 weight=1 max_fails=1 fail_timeout=60s;

                           }

    upstream phpserver {

                          server 192.168.62.157:80 weight=1 max_fails=1 fail_timeout=60s;

                         }

                server {

                listen 80;

    server_name localhost;

    #动态资源加载

    location ~ .(php|jsp)$ {

    proxy_pass http://phpserver;

    proxy_set_header Host $host:$server_port; proxy_set_header X-Real-IP $remote_addr;

    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

                                   }

    #静态资源加载

    location ~ .*.(html|gif|jpg|png|bmp|swf|css|js)$ {

    proxy_pass http://static;

    proxy_set_header Host $host:$server_port; proxy_set_header X-Real-IP $remote_addr;

    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

                                  }

    }

    192.168.62.155 静态资源

    #静态资源配置 主配置文件-include /etc/nginx/conf.d/*.conf

    # vim /etc/nginx/conf.d/static.conf

    server {

                    listen 80;

                     server_name localhost;

                    location ~ .(html|jpg|png|js|css|gif|bmp|jpeg) {

                    root /home/www/nginx;

                    index index.html index.html;

                  }

    }

    [root@nginx-server2 nginx]# cat /home/www/nginx/index.html //模拟静态资源

    hello 155
    192.168.62.157 动态资源

    #动态资源配置: yum 安装php7.1

    [root@nginx-server ~]# rpm -Uvh https://mirror.webtatic.com/yum/el7/epel-release.rpm
    [root@nginx-server ~]# rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm

    [root@nginx-server ~]# yum install php71w-xsl php71w php71w-ldap php71w-cli php71w-common php71w-devel php71w-gd php71w-pdo php71w-mysql php71w-mbstring php71w-bcmath php71w-mcrypt y

    [root@nginx-server ~]# yum install -y php71w-fpm [root@nginx-server ~]# systemctl start php-fpm [root@nginx-server ~]# systemctl enable php-fpm

    编辑nginx的配置文件:

    server {

    listen 80;

    server_name localhost;

    location ~ .php$ { root /home/nginx/html;                      #指定网站目录

    fastcgi_pass 127.0.0.1:9000;                                         #指定访问地址

    fastcgi_index index.php;                                                 #指定默认文件

    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;                               #站点根目录,取 决于root配置项

    include fastcgi_params;                                                 #包含nginx常量定义

                                                   }

    }

    [root@nginx-server1 html]# cat /home/nginx/html/index.php            //模拟动态资源

    dongtai
    当访问静态页面的时候location 匹配到 (html|jpg|png|js|css|gif|bmp|jpeg) 通过转发到静态服务器,静态服务通 过location的正则匹配来处理请求。

    当访问动态页面时location匹配到 .php 结尾的文件转发到后端php服务处理请求。

  • 相关阅读:
    关于色彩空间 color space的faq
    opensuse11 DNS不稳定的问题
    Streaming MPEG4 with Linux
    在RELEASE版本中快速定位DATA ABORT的方法 zt
    YUV / RGB 格式及快速转换算法zt
    角色转变——从工程师到项目经理(转)
    coredll.lib(COREDLL.dll) : fatal error LNK1112: module machine type 'THUMB' conflicts with target machine type 'ARM'
    勿使用浮点运算
    DirectShow中常见的RGB/YUV格式
    基于Linux系统核心的汉字显示的尝试zt
  • 原文地址:https://www.cnblogs.com/wyglog/p/12459472.html
Copyright © 2020-2023  润新知