nginx怎样实现动静分离

  介绍

这篇文章将为大家详细讲解有关nginx怎样实现动静分离,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

为了加快网站的解析速度,可以把动态页面和静态页面由不同的服务器来解析,加快解析速度,降低原来单个服务器的压力。简单来说,就是使用正则表达式匹配过滤,然后交个不同的服务器。

<强> 1,准备环境

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

1。配置编译安装的nginx为反向代理上游;

upstream  static  {   server  10.0.105.196:80 体重=1,max_fails=1, fail_timeout=60年代;   }   upstream  php  {   server  10.0.105.200:80 体重=1,max_fails=1, fail_timeout=60年代;   }      server  {   listen  server_name   #动态资源加载   80;   本地主机   的位置~ \。(php | jsp)美元,{,proxy_pass  http://phpserver;      主持人:美元proxy_set_header  Host  server_port;, proxy_set_header  X-Real-IP  remote_addr美元;   proxy_set_header  X-Forwarded-For  proxy_add_x_forwarded_for美元;   }      #静态资源加载   的位置~ \。(html css | | jpg | png | js)美元,{,proxy_pass  http://static, proxy_set_header  Host 主持人:美元server_port美元;,proxy_set_header  X-Real-IP  remote_addr美元;   proxy_set_header  X-Forwarded-For  proxy_add_x_forwarded_for美元;   }   }

静态资源配置——10.0.105.196

server  {   listen  80;   server_name 本地主机;      的位置~ \。(html | | png jpg图片| js | css)美元,{,root /var/www/nginx;   }   }

上传图片

动态资源配置:10.0.105.200

百胜安装php7.1

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

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

[root@nginx-server ~] # yum安装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安装- y php71w-fpm [root@nginx-server ~] # systemctl开始php-fpm [root@nginx-server ~] # systemctl启用php-fpm

编辑nginx的配置文件:

root@nginx-server ~ # cd/etc/nginx/conf.d/[root@nginx-server conf.d] # vim phpserver。配置服务器{

听80;

server_name主机;~ \位置。php $ {

根/home/nginx/html;#指定网站目录

fastcgi_pass fastcgi_index fastcgi_param

#站点根目录,取决于根配置项包括

}

}

127.0.0.1:9000;#指定访问地址

index . php,

#指定默认文件

SCRIPT_FILENAME document_root fastcgi_script_name美元;

fastcgi_params;#包含nginx常量定义

当访问静态页面的时候位置匹配到(html | | png jpg图片| js | css)通过转发到静态服务器,静态服务通过

位置的正则匹配来处理请求。

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

知识点扩展:

通过请求分离

[root@lb01 相依]#,vim  nginx.conf   worker_processes  1;   events  {   worker_connections 才能;1024;   }   http  {   include 才能,,,,mime.types;   default_type 才能;应用程序/八进制;   sendfile 才能,,,,,   keepalive_timeout 才能;65;   upstream  stack_pools  {   ,,,server  172.25.254.134:80 体重=5;   }   upstream  dynamic_pools  {   ,,,server  172.25.254.135:80 体重=5;   }   server {才能   ,,,listen ,,, 80;   ,,,server_name  www.lbtest.com;   ,,,的位置/,{   ,,,,,root , html;   ,,,,,index  index.html 你可以;   ,,,,,proxy_set_header  Host  $主机;   ,,,,,proxy_pass  http://dynamic_pools;   ,,,}   ,,,的位置/图像/,{   ,,,,,proxy_set_header  Host  $主机;   ,,,proxy_pass  http://stack_pools;   ,,,}   ,,,的位置/动态/,{   ,,,,,proxy_set_header  Host  $主机;   ,,,proxy_pass  http://dynamic_pools;   ,,,}   ,,}   }   [root@lb01 相依]#,nginx  -s 重载

根据扩展名分离

[root@lb01 相依]#,vim  nginx.conf   ,   worker_processes  1;   events  {   worker_connections 才能;1024;   }   http  {   include 才能,,,,mime.types;   default_type 才能;应用程序/八进制;   sendfile 才能,,,,,   keepalive_timeout 才能;65;   upstream  stack_pools  {   ,,,server  172.25.254.134:80 体重=5;   }   upstream  dynamic_pools  {   ,,,server  172.25.254.135:80 体重=5;   }   server {才能   ,,,listen ,,, 80;   ,,,server_name  www.lbtest.com;   ,,,的位置/,{   ,,,,,root , html;   null   null   null   null   null   null   null   null   null   null   null

nginx怎样实现动静分离