nginx反向代理缓存静态文件

  
  
  
  
  1. 环境: 
  2. nginx   反向代理    172.16.0.100 
  3. apache1 web     172.16.0.110 
  4. apache2 web     172.16.0.120 
  5.  
  6. 目的:网站静态文件分离为多个域名,每个域名分别在两台apache上做负载均衡,nginx缓存静态文件,并实现故障转移…… 
  7.  
  8. -------------------------------------------- 
  9. nginx配置: 
  1. vim /usr/local/nginx/conf/proxy.conf
  2. proxy_redirect off;
  3. proxy_set_header Host $host;
  4. proxy_set_header X-Real-IP $remote_addr;
  5. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  6. proxy_ignore_client_abort on;
  7. proxy_connect_timeout 90;
  8. proxy_send_timeout 90; proxy_read_timeout 90;
  9. proxy_next_upstream error timeout invalid_header http_500 http_503 http_404;
  10. proxy_set_header Accept-Encoding '';
  11.  
  12. vim /usr/local/nginx/conf/nginx.conf
  13. server{ 
  14.         listen 80; 
  15.     server_name jpg.test.com; 
  16.     root /data/webroot/jpg; 
  17.  
  18.     location ~* .*\.(jpg|gif|js|css|html|htm)$ { 
  19.         expires 1d; 
  20.         proxy_store on
  21.         proxy_store_access user:rw group:r all:r; 
  22.         proxy_temp_path /data/webroot/jpg; 
  23.         include proxy.conf;
  24. if ( !-e $request_filename) { 
  25.         proxy_pass  http://jpg;}} 
  26.  
  27. server{ 
  28.         listen 80; 
  29.         server_name jpg1.test.com; 
  30.         root /data/webroot/jpg1; 
  31.  
  32.         location ~* .*\.(jpg|gif|js|css|html|htm)$ { 
  33.                 expires 1d; 
  34.                 proxy_store on
  35.                 proxy_store_access user:rw group:r all:r; 
  36.                 proxy_temp_path /data/webroot/jpg1;          
  37. include proxy.conf;
  38.                 if ( !-e $request_filename) { 
  39.                 proxy_pass  http://jpg1;}} 
  40.  
  41. server{ 
  42.         listen 80; 
  43.         server_name jpg2.test.com; 
  44.         root /data/webroot/jpg2; 
  45.  
  46.         location ~* .*\.(jpg|gif|js|css|html|htm)$ { 
  47.                 expires 1d; 
  48.                 proxy_store on
  49.                 proxy_store_access user:rw group:r all:r; 
  50.                 proxy_temp_path /data/webroot/jpg2; 
  51.           include proxy.conf;
  52.                 if ( !-e $request_filename) { 
  53.                 proxy_pass  http://jpg2;}} 
  54.  
  55. server{ 
  56.         listen 80; 
  57.         server_name jpg3.test.com; 
  58.         root /data/webroot/jpg3; 
  59.  
  60.         location ~* .*\.(jpg|gif|js|css|html|htm)$ { 
  61.                 expires 1d; 
  62.                 proxy_store on
  63.                 proxy_store_access user:rw group:r all:r; 
  64.                 proxy_temp_path /data/webroot/jpg3; 
  65.           include proxy.conf;
  66.                 if ( !-e $request_filename) { 
  67.                 proxy_pass  http://jpg3;}} 
  68.  

  69.  
  70.  
  71. upstream jpg { 
  72.      server 172.16.0.110:80 max_fails=3 fail_timeout=30s; 
  73.      server 172.16.0.120:80 max_fails=3 fail_timeout=30s; 
  74. upstream jpg1 { 
  75.      server 172.16.0.110:81 max_fails=3 fail_timeout=30s; 
  76.      server 172.16.0.120:81 max_fails=3 fail_timeout=30s; 
  77. upstream jpg2 { 
  78.      server 172.16.0.110:82 max_fails=3 fail_timeout=30s; 
  79.      server 172.16.0.120:82 max_fails=3 fail_timeout=30s; 
  80. upstream jpg3 { 
  81.      server 172.16.0.110:83 max_fails=3 fail_timeout=30s; 
  82.      server 172.16.0.120:83 max_fails=3 fail_timeout=30s; 
  83.  
  84. --------------------------------------------- 
  85.  
  86. apache都使用以下配置:注意使用端口区分!!! 
  87.  
  88. <VirtualHost *:80> 
  89.     ServerAdmin [email protected] 
  90.     DocumentRoot "/data/webroot/jpg" 
  91.     ServerName jpg.test.com 
  92. </VirtualHost> 
  93.  
  94. <VirtualHost *:81> 
  95.     ServerAdmin [email protected] 
  96.     DocumentRoot "/data/webroot/jpg" 
  97.     ServerName jpg1.test.com 
  98. </VirtualHost> 
  99.  
  100. <VirtualHost *:82> 
  101.     ServerAdmin [email protected] 
  102.     DocumentRoot "/data/webroot/jpg2" 
  103.     ServerName jpg2.test.com 
  104. </VirtualHost> 
  105.  
  106. <VirtualHost *:83> 
  107.     ServerAdmin [email protected] 
  108.     DocumentRoot "/data/webroot/jpg3" 
  109.     ServerName jpg3.test.com 
  110. </VirtualHost> 
  111.  
  112. ------------------------------------------------------------------------------------------- 

  113. 总结: 
  114. 1、防盗链 
  115. 如果apache使用了图片防盗链,则nginx不用再做防盗链配置 
  116. 2、apache 
  117.  
  118. if ( !-e $request_filename) { 
  119.     proxy_pass  http://172.16.0.110:83;}
  120. 还可以改/etc/hosts文件,解析域名到对应后端web服务器。建议使用端口方式!
  121. 4、关于proxy_set_header Accept-Encoding ''
  122. nginx 向后端请求 gzip 内容 
  123. proxy_set_header Accept-Encoding 'gzip'
  124. nginx 向后端请求未 gzip 内容 
  125. proxy_set_header Accept-Encoding ''
  126. 5、nginx定义服务器集时最好使用端口定义不同服务器
  127. 6、 proxy_temp_path定义的目录nginx为了有写入权限,会在重启nginx时自动把此目录属主改为nginx进程用户
  128. 7、proxy_next_upstream设定了相应条件后,nginx只查寻一遍,不会无限制查询

 

你可能感兴趣的:(nginx,反向代理,缓存静态文件)