nginx 实现动静分离

目录

[nginx 实现动静分离](#nginx 实现动静分离)

1.配置动静分离


nginx 实现动静分离

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

1.配置动静分离

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

cpp 复制代码
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      80;
        server_name     localhost;
        #动态资源加载
        location ~ \.(php|jsp)$ {
            proxy_pass http://php;
            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;
                }
        }
cpp 复制代码
静态资源配置
server {
        listen 80;
        server_name     localhost;

        location ~ \.(html|jpg|png|js|css|gif|bmp|jpeg) {
        root /home/www/nginx;
        }
}
cpp 复制代码
动态资源配置:
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 php74-php-xsl php74-php php74-php-ldap php74-php-cli php74-php-common php74-php-devel php74-php-gd php74-php-pdo php74-php-mysql php74-php-mbstring php74-php-bcmath php74-php-mcrypt -y
[root@nginx-server ~]#yum install -y php74-php-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常量定义
        		}
        }

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

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

相关推荐
CodexDave6 小时前
MySQL事务隔离级别与MVCC机制解析
前端·数据库·mysql·nginx·性能优化·负载均衡
敖行客 Allthinker8 小时前
Parallels Ubuntu虚拟机项目如何让手机访问?完整解决方案
linux·运维·ubuntu
元Y亨H10 小时前
生产环境监控与故障应急处理(5-5-5 标准)指南
运维
观山岳五楼11 小时前
Ubuntu 24 怎么使用Ubuntu 20 的镜像源
linux·运维·ubuntu
寒晓星11 小时前
[linux]线程及多线程
linux·运维
慧一居士12 小时前
Linux如何设置固定的静态ip
运维
国服第二切图仔13 小时前
13其他工具 - Skill/LSP/Sleep等
linux·运维·里氏替换原则
暴力求解14 小时前
Linux ---线程控制(二)
linux·运维·服务器·操作系统
梦痕长情15 小时前
等保三级三权账户
linux·运维·服务器
jiecy16 小时前
为Linux系统双网卡配置静态路由
linux·运维