nginx实现动静分离实例讲解
为了加快网站的解析速度,可以把动态页面和静态页面由不同的服务器来解析,加快解析速度。降低原来单个服务器的压力。简单来说,就是使用正则表达式匹配过滤,然后交个不同的服务器。
1、准备环境
准备一个nginx代理两个http分别处理动态和静态。
1.配置编译安装的nginx为反向代理upstream;
upstreamstatic{ server10.0.105.196:80weight=1max_fails=1fail_timeout=60s; } upstreamphp{ server10.0.105.200:80weight=1max_fails=1fail_timeout=60s; } server{ listenserver_name #动态资源加载 80; localhost location~\.(php|jsp)${proxy_passhttp://phpserver; proxy_set_headerHost$host:$server_port;proxy_set_headerX-Real-IP$remote_addr; proxy_set_headerX-Forwarded-For$proxy_add_x_forwarded_for; } #静态资源加载 location~\.(html|jpg|png|css|js)${proxy_passhttp://static;proxy_set_headerHost$host:$server_port;proxy_set_headerX-Real-IP$remote_addr; proxy_set_headerX-Forwarded-For$proxy_add_x_forwarded_for; } }
静态资源配置---10.0.105.196
server{ listen80; server_namelocalhost; location~\.(html|jpg|png|js|css)${root/var/www/nginx; } }
上传图片
动态资源配置:10.0.105.200
yum安装php7.1
[root@nginx-server~]#rpm-Uvhhttps://mirror.webtatic.com/yum/el7/epel-release.rpm
[root@nginx-server~]#rpm-Uvhhttps://mirror.webtatic.com/yum/el7/webtatic-release.rpm
[root@nginx-server~]#yuminstallphp71w-xslphp71wphp71w-ldapphp71w-cliphp71w-commonphp71w-develphp71w-gdphp71w-pdophp71w-mysqlphp71w-mbstringphp71w-bcmathphp71w-mcrypt-y
[root@nginx-server~]#yuminstall-yphp71w-fpm[root@nginx-server~]#systemctlstartphp-fpm[root@nginx-server~]#systemctlenablephp-fpm
编辑nginx的配置文件:
[root@nginx-server~]#cd/etc/nginx/conf.d/[root@nginx-serverconf.d]#vimphpserver.confserver{
listen80;
server_namelocalhost;location~\.php${
root/home/nginx/html;#指定网站目录
fastcgi_passfastcgi_indexfastcgi_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@lb01conf]#vimnginx.conf worker_processes1; events{ worker_connections1024; } http{ include mime.types; default_typeapplication/octet-stream; sendfile on; keepalive_timeout65; upstreamstack_pools{ server172.25.254.134:80weight=5; } upstreamdynamic_pools{ server172.25.254.135:80weight=5; } server{ listen 80; server_namewww.lbtest.com; location/{ root html; indexindex.htmlindex.htm; proxy_set_headerHost$host; proxy_passhttp://dynamic_pools; } location/image/{ proxy_set_headerHost$host; proxy_passhttp://stack_pools; } location/dynamic/{ proxy_set_headerHost$host; proxy_passhttp://dynamic_pools; } } } [root@lb01conf]#nginx-sreload
根据扩展名分离
[root@lb01conf]#vimnginx.conf worker_processes1; events{ worker_connections1024; } http{ include mime.types; default_typeapplication/octet-stream; sendfile on; keepalive_timeout65; upstreamstack_pools{ server172.25.254.134:80weight=5; } upstreamdynamic_pools{ server172.25.254.135:80weight=5; } server{ listen 80; server_namewww.lbtest.com; location/{ root html; indexindex.htmlindex.htm; proxy_set_headerHost$host; proxy_passhttp://dynamic_pools; } location~.*.(jpg|png|gif|css|js|swf|bmp|jsp|php|asp)${ proxy_set_headerHost$host; proxy_passhttp://stack_pools; } } } [root@lb01conf]#nginx-sreload
到此这篇关于nginx实现动静分离实例讲解的文章就介绍到这了,更多相关nginx实现动静分离内容请搜索毛票票以前的文章或继续浏览下面的相关文章希望大家以后多多支持毛票票!
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。