做网站维护工作难吗,公司注册查询重名,郑州百度建网站,建设部网站 绿色建筑评价表一、为什么要进行动静分离
分离资源#xff0c;减少不必要到的请求消耗#xff0c;减少请求延时。
注#xff1a;我这里#xff0c;是nginx处理静态资源#xff0c;apache处理动态资源。
场景分析#xff1a;
1、未分离之前的场景步骤
#xff08;1#xff09;客户…一、为什么要进行动静分离
分离资源减少不必要到的请求消耗减少请求延时。
注我这里是nginx处理静态资源apache处理动态资源。
场景分析
1、未分离之前的场景步骤
1客户端请求url到中间件比如nginxapache
2中间件根据url请求相应目录程序框架
3程序框架运行程序逻辑
4程序逻辑请求相应数据资源
5将数据资源返回给客户端
注其实静态资源是不需要经过动态请求直接中间件返回给客户端就可以了。也就是说只要第1步和第5步就可以了 配置文件展示
upstream php_api{#代理请求到本地apache服务器实现动静分离这里我将apache默认端口更改为81server 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.htmlerror_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/xmlrss 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
注负载均衡中多态服务器间的数据同步这里采用rsync当然还有其他方式。可参考
https://www.cnblogs.com/miclesvic/p/6189540.html