nginx实现动静分离实例讲解

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

1、准备环境

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

1.配置编译安装的nginx为反向代理upstream;

  upstream static {  server 10.0.105.196:80 weight=1 max_fails=1 fail_timeout=60s;  }  upstream php {  server 10.0.105.200:80 weight=1 max_fails=1 fail_timeout=60s;  }    server {  listen server_name  #动态资源加载  80;  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|jpg|png|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;  }  }

静态资源配置---10.0.105.196

  server {  listen 80;  server_name localhost;    location ~ .(html|jpg|png|js|css)$ { root /var/www/nginx;  }  }

上传图片

动态资源配置: 10.0.105.200

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的配置文件:

[root@nginx-server ~]# cd /etc/nginx/conf.d/ [root@nginx-server conf.d]# vim phpserver.conf server {

listen 80;

server_name localhost; location ~ .php$ {

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

fastcgi_pass fastcgi_index fastcgi_param

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

include

}

}

127.0.0.1:9000; #指定访问地址

index.php;

#指定默认文件

SCRIPT_FILENAME $document_root$fastcgi_script_name;

fastcgi_params; #包含nginx常量定义

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

location的正则匹配来处理请求。

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

知识点扩展:

通过请求分离

  [root@lb01 conf]# vim nginx.conf  worker_processes 1;  events {    worker_connections 1024;  }  http {    include    mime.types;    default_type application/octet-stream;    sendfile    on;    keepalive_timeout 65;  upstream stack_pools {      server 172.25.254.134:80 weight=5;  }  upstream dynamic_pools {      server 172.25.254.135:80 weight=5;  }    server {      listen    80;      server_name www.lbtest.com;      location / {        root  html;        index index.html index.htm;        proxy_set_header Host $host;        proxy_pass http://dynamic_pools;      }      location /image/ {        proxy_set_header Host $host;      proxy_pass http://stack_pools;      }      location /dynamic/ {        proxy_set_header Host $host;      proxy_pass http://dynamic_pools;      }    }  }  [root@lb01 conf]# nginx -s reload

根据扩展名分离

  [root@lb01 conf]# vim nginx.conf     worker_processes 1;  events {    worker_connections 1024;  }  http {    include    mime.types;    default_type application/octet-stream;    sendfile    on;    keepalive_timeout 65;  upstream stack_pools {      server 172.25.254.134:80 weight=5;  }  upstream dynamic_pools {      server 172.25.254.135:80 weight=5;  }    server {      listen    80;      server_name www.lbtest.com;      location / {        root  html;        index index.html index.htm;        proxy_set_header Host $host;        proxy_pass http://dynamic_pools;      }      location ~ .*.(jpg|png|gif|css|js|swf|bmp|jsp|php|asp)$ {      proxy_set_header Host $host;      proxy_pass http://stack_pools;      }    }  }  [root@lb01 conf]# nginx -s reload

到此这篇关于nginx实现动静分离实例讲解的文章就介绍到这了,更多相关nginx实现动静分离内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

参与评论