码迷,mamicode.com
首页 > 其他好文 > 详细

动静分离

时间:2017-11-17 13:26:12      阅读:121      评论:0      收藏:0      [点我收藏+]

标签:end   tab   web服务   mime   index   目录   timeout   安全   提前   

1. 案例背景

通过Nginx实现动静分离,即通过Nginx反向代理配置规则实现让动态资源和静态资源及其他业务分别由不同的服务器解析,以解决网站性能、安全、用户体验等重要问题。

 

2. 需求分析

  • 当用户请求www.xindaichina.cn/upload/xx地址时,实现由upload上传服务器池处理请求。
  • 当用户请求www.xindaichina.cn/static/xx地址时,实现由静态服务器池处理请求
  • 对于访问其它请求,都由默认的动态服务器池处理请求
 

3. 规划

节点IP及端口地址显示结果
web01 10.0.0.8:80 http://www.xindaichina.cn/static/index.html static
web02 10.0.0.7:80 http://www.xindaichina.cn/index.html default
web03 10.0.0.9:80 http://www.xindaichina.cn/upload/index.html upload
 

4. 部署

 

4.1 upstream地址池配置

 
  1. upstream static_server_pools {
  2. server 10.0.0.8:80 weight=1;
  3. }
  4. upstream default_server_pools {
  5. server 10.0.0.7:80 weight=1;
  6. }
  7. upstream upload_server_pools {
  8. server 10.0.0.9:80 weight=1;
  9. }
 

4.2 location或if语句方案

location方案

在server{}标签中添加

 
  1. location /static/ {
  2. proxy_pass http://static_pools;
  3. include proxy.conf;
  4. }
  5. location / {
  6. proxy_pass http://default_pools;
  7. include proxy.conf;
  8. }
  9. location /upload/ {
  10. proxy_pass http://upload_pools;
  11. include proxy.conf;
  12. }

if语句方案

 
  1. if ($request_uri ~* "^/static/(.*)$")
  2. {
  3. proxy_pass http://static_pools/$1;
  4. }
  5. if ($request_uri ~* "^/upload/(.*)$")
  6. {
  7. proxy_pass http://upload_pools/$1;
  8. }
  9. location / {
  10. proxy_pass http://default_pools;
  11. include proxy.conf;
  12. }
 

4.3 Web服务器配置

在对应的web服务器中配置对应的目录和index.html文件 
web01

 
  1. mkdir static
  2. echo "static" >static/index.html

web02

 
  1. echo "default" >index.html

web03

 
  1. mkdir upload
  2. echo "upload" >upload/index.html
 

4.4 测试

 
  1. curl www.xindaichina.cn/static/index.html
  2. curl www.xindaichina.cn/index.html
  3. curl www.xindaichina.cn/upload/index.html
 

5. 根据客户端的设备来转发

 
    1. location / {
    2. if ($http_user_agent ~* "android")
    3. {
    4. proxy_pass http://android_pools; #<==这是android服务器池,需要提前定义upstream。
    5. }
    6. if ($http_user_agent ~* "iphone")
    7. {
    8. proxy_pass http://iphone_pools; #<==这是iphone服务器池,需要提前定义upstream。
    9. }
    10. proxy_pass http://pc_pools;
    11. include extra/proxy.conf;
    12. }
    13. ##nginx.conf配置
    14. worker_processes 1;
    15. events {
    16. worker_connections 1024;
    17. }
    18. http {
    19. include mime.types;
    20. default_type application/octet-stream;
    21. sendfile on;
    22. keepalive_timeout 65;
    23. upstream android_pools {
    24. server 10.0.0.7:80 weight=1;
    25. }
    26. upstream iphone_pools {
    27. server 10.0.0.7:8080 weight=1;
    28. }
    29. upstream pc_pools {
    30. server 10.0.0.8:80 weight=1;
    31. }
    32. server {
    33. listen 80;
    34. server_name www.xindaichina.cn;
    35. location / {
    36. if ($http_user_agent ~* "android")
    37. {
    38. proxy_pass http://android_pools;
    39. }
    40. if ($http_user_agent ~* "iphone")
    41. {
    42. proxy_pass http://iphone_pools;
    43. }
    44. proxy_pass http://pc_pools;
    45. include proxy.conf;
    46. }
    47. }
    48. }

动静分离

标签:end   tab   web服务   mime   index   目录   timeout   安全   提前   

原文地址:http://www.cnblogs.com/wanglan/p/7850939.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!