网站首页 > 技术教程 正文
一、为什么要进行动静分离
分离资源,减少不必要到的请求消耗,减少请求延时。
注:我这里,是nginx处理静态资源,apache处理动态资源。
场景分析:
1、未分离之前的场景步骤
(1)客户端请求url到中间件(比如nginx,apache)
(2)中间件根据url请求相应目录,程序框架
(3)程序框架运行程序逻辑
(4)程序逻辑请求相应数据资源
(5)将数据资源返回给客户端
注:其实,静态资源是不需要经过动态请求,直接中间件返回给客户端就可以了。也就是说只要第1步和第5步就可以了
配置文件展示:
upstream php_api{ #代理请求到本地apache服务器,实现动静分离(这里我将apache默认端口更改为81) server 127.0.0.1:81; } server { listen 80; server_name www.xiaobudiu.top; access_log /etc/nginx/logs/access/www.xiabudiu.top.access.log main; root /data/www; location ~ \.php$ { #如果网站访问的url后缀是.php,则代理使用apache进行解析 proxy_pass http://php_api; index index.html index.htm; } #如果请求的是静态资源,则默认使用nginx进行处理 location ~ \.(jpg|png|gif)$ { expires 1h; gzip on; } location /{ index index.html index.htm; } # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 404 403 /404.html; location = /404.html { root /data/errorPage; } location ~ /\.ht { deny all; } }
或者是这样:
upstream image { server 192.168.0.3:80; server 192.168.0.4:80; } upstream php { server 192.168.0.5:80; server 192.168.0.6:80; } server { listen 80; server_name www.xiaobudiu.top; access_log /etc/nginx/logs/access/www.xiabudiu.top.access.log main; location /{ #如果uri后缀不是.php或是图片后缀,就走本地服务器进行处理 root data/www; index index.php index.html; } location ~* \.php$ { #如果是.php结尾,反向代理到upstream php组里进行轮询 proxy_pass http://php; } location ~* "\.(.jpg|png|jpeg|gif)" { #如果是.jpg,.png,.jpeg,.gif结尾,反向代理到upstream image组里进行轮询 proxy_pass http://image; } # redirect server error pages to the static page /404.html error_page 500 502 503 504 404 403 /404.html; location = /404.html { root /data/errorPage; } location ~ /\.ht { deny all; } }
注:这是在子配置文件中进行的定义,比如,上面编辑的就是/etc/nginx/conf.d/www.xiaobudiu.top.conf 文件
当然,由于nginx对代理有一定要求,所以,在nginx.conf中也要进行一定的定义,比如这样:
nginx.conf
user nginx; worker_processes 1; worker_rlimit_nofile 65536; error_log /etc/nginx/logs/error/error.log warn; pid /var/run/nginx.pid; events { worker_connections 1024; multi_accept on; use epoll; } http { include /etc/nginx/mime.types; default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log /etc/nginx/logs/access/access.log main; sendfile on; #tcp_nopush on; keepalive_timeout 65; client_max_body_size 20m; gzip on; gzip_proxied any; gzip_comp_level 3; gzip_min_length 1k; gzip_buffers 16 32k; gzip_http_version 1.0; gzip_types text/plain text/css application/json application/xml+rss text/javascript image/jpeg image/gif image/png; fastcgi_buffers 256 16k; fastcgi_buffer_size 128k; fastcgi_connect_timeout 3s; fastcgi_send_timeout 120s; fastcgi_read_timeout 120s; reset_timedout_connection on; server_names_hash_bucket_size 100; include /etc/nginx/conf.d/*.conf; }
最后,需要说明的是,上述配置文件只是为了说明反向代理和负载均衡是如何实现的,并没有结合实际项目。
注:nginx 官方proxy模块文档 http://nginx.org/en/docs/http/ngx_http_proxy_module.html
猜你喜欢
- 2024-10-11 Python运维:Nginx作为七层负载均衡调度器
- 2024-10-11 nginx的反向代理(nginx的反向代理模块是啥)
- 2024-10-11 Nginx学习笔记(14) 用于定义上游服务的upstream模块
- 2024-10-11 入口网关服务发现-Openresty动态upstream
- 2024-10-11 nginx可以集群吗(nginx集群 并绑定一个ip)
- 2024-10-11 nginx的upstream目前支持5种方式的分配
- 2024-09-21 详解:Nginx 反向代理、后端检测模块
- 2024-09-21 Nginx反向代理相关设置(基于linux)
- 2024-09-21 简单练习Nginx反向代理和负载均衡
- 2024-09-21 nginx upstream header过大问题解决
你 发表评论:
欢迎- 最近发表
-
- Linux新手必看:几种方法帮你查看CPU核心数量
- linux基础命令之lscpu命令(linux中ls命令的用法)
- Linux lscpu 命令使用详解(linux常用ls命令)
- 如何查询 Linux 中 CPU 的数量?这几个命令要知道!
- 在linux上怎么查看cpu信息(linux如何查看cpu信息)
- 查看 CPU 的命令和磁盘 IO 的命令
- 如何在CentOS7上改变网卡名(centos怎么改网卡名字)
- 网工必备Linux网络管理命令(网工必备linux网络管理命令是什么)
- Linux 网络命令知多少(linux 网络 命令)
- Linux通过命令行连接wifi的方式(linux命令行连接无线网)
- 标签列表
-
- 下划线是什么 (87)
- 精美网站 (58)
- qq登录界面 (90)
- nginx 命令 (82)
- nginx .http (73)
- nginx lua (70)
- nginx 重定向 (68)
- Nginx超时 (65)
- nginx 监控 (57)
- odbc (59)
- rar密码破解工具 (62)
- annotation (71)
- 红黑树 (57)
- 智力题 (62)
- php空间申请 (61)
- 按键精灵 注册码 (69)
- 软件测试报告 (59)
- ntcreatefile (64)
- 闪动文字 (56)
- guid (66)
- abap (63)
- mpeg 2 (65)
- column (63)
- dreamweaver教程 (57)
- excel行列转换 (56)
本文暂时没有评论,来添加一个吧(●'◡'●)