• 如何实现Nginx动静分离


    前言

    动静分离,就是将css、js、jpg等静态资源和jsp等动态资源分开处理,以此提高服务器响应速度,提高性能。

    一、Nginx动静分离部署

     1.1 准备环境

    1 192.168.13.129     代理服务器
    2 192.168.13.133     静态资源
    3 192.168.13.128     动态资源
    4 三台机器都需要关闭防火墙和selinux,每次修改完配置文件需要
    5 # nginx -s reload
    6 重新加载生效

    1.2 配置代理服务器(192.168.13.129)

    [root@nginx ~]# vim /etc/nginx/conf.d/default.conf  
    添加以下代码(需要你修改的只是ip地址)
    upstream static {
            server 192.168.13.133:80 weight=1 max_fails=1 fail_timeout=60s;
            }
    upstream phpserver {
            server 192.168.13.128:80 weight=1 max_fails=1 fail_timeout=60s;
            }
         server {
            listen      80;
            server_name     localhost;
            #动态资源加载(以.php或.jsp结尾的都会访问到这个地址)
            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;
                    }
            #静态资源加载(以.(html|gif|jpg|png|bmp|swf|css|js)结尾的都会访问到这个地址)
            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;
                    }
            }

    1.3 静态资源配置(192.168.13.133这台服务器)

    [root@real ~]# vim /etc/nginx/conf.d/default.conf
    添加以下代码
    server {
            listen 80;
            server_name     localhost;
    
            location ~ .(html|jpg|png|js|css|gif|bmp|jpeg) {
            root /home/www/nginx;  #/home/www/nginx 自定义路径,需要自己创建
            index index.html index.html;
            }
    }

      创建目录及文件并写入测试数据

    [root@real ~]# mkdir -p /home/www/nginx    #创建访问目录
    [root@real ~]# echo "this is static" > /home/www/nginx/index.html        #写入测试数据

    1.4 动态资源配置(192.168.13.128这台服务器)

    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 -y install php71w-xsl php71w php71w-ldap php71w-cli php71w-common php71w-devel php71w-gd php71w-pdo php71w-mysql php71w-mbstring php71w-bcmath php71w-mcrypt php71w-fpm
    [root@nginx-server ~]# systemctl start php-fpm  
    [root@nginx-server ~]# systemctl enable php-fpm  #设置开机自启
    [root@nginx-server ~]# /etc/nginx/conf.d/default.conf
    添加以下代码
    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-server ~]# mkdir -p /home/nginx/html
    [root@nginx-server ~]# echo "this is dynamic" > /home/nginx/html/index.php       #写入测试数据

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

    当访问动态页面时location匹配到 .php 结尾的文件转发到后端php服务处理请求,以此来实现动静分离。

    道阻且长,行则将至!加油! --不是冷漠
  • 相关阅读:
    读书笔记:7个示例科普CPU Cache
    no such partition grub rescue>
    这些个云盘
    原版win7镜像IE主页被篡改?
    JS判断访问设备、客户端操作系统类型
    floodlight make the VMs can not getDHCP IP address
    MPI之聚合通信-Scatter,Gather,Allgather
    MPI 环境搭建问题-运行程序闪退
    【算法、递归回溯解决数独】
    算法【最大子序列问题】
  • 原文地址:https://www.cnblogs.com/bushilengmo/p/13912756.html
Copyright © 2020-2023  润新知