nginx_3_反向代理负载均衡缓存URL重写

LEMP
  nginx(FastCgi)+php-fpm

  location ~* \.php$ {
        fastcgi_pass  127.0.0.1:9000;
       }


nginx配置文件
main,
  worker_process
  error_log
  user
  group

events {
  }

http {
  server{
       listen 80;
       server_name test.hale.com;
       location / {
         root html;
         index index.html;
         allow 192.168.21.85;
         deny all;
         auth_basic    "The authentication"
         auth_basic_user_file /usr/local/nginx/.user
        }
    }
 }


#定义默认主机头,即没有明确定义虚拟主机的都使用此server
  server {
       listen 80 default;
       return 403;
      }




nginx反向代理:

proxy_pass

  server {
       listen 80;
       server_name test.hale.com;
       location / {
         proxy_pass http://192.168.21.123:80;
         proxy_set_header X-Real-IP $remote_addr;
       }
   }



nginx负载均衡:

upstream

#定义上游服务器组
   upstream 123test {
        #ip_hash;
        server 192.168.21.123:80 weight=1 max_fails=2 fail_timeout=3;
        server 192.168.21.124:80 weight=1 max_fails=2 fail_timeout=3;
        server 127.0.0.1:8080 backup;  #定义sorry页面,不能与ip_hash算法一起使用
    }


   server {
        listen 80;
        server_name 123test.hale.com;
        location / {
          proxy_pass http://123test;
          proxy_set_header X-Real-IP $remote_addr;
        }
     }


#定义sorry虚拟主机,在所有上游服务器不可用时,代理到该虚拟主机
   server {
        listen  8080;
        server_name localhost;
        location /{
          root /usr/local/nginx/html;
          index sorry.html;
         }
       }




nginx缓存:
  共享内存: 存储键和缓存对象元数据
  磁盘空间: 存储数据

  proxy_cache_path: 不能定义在server{}中
       
   proxy_cache_path /nginx/cache/first levels=1:2:1 keys_zone=first:32m max_size=1g;

   upstream 123test {
        #ip_hash;
        server 192.168.21.123:80 weight=1 max_fails=2 fail_timeout=3;
        server 192.168.21.124:80 weight=1 max_fails=2 fail_timeout=3;
        #server 127.0.0.1:8080 backup;
    }


   server {
        listen 80;
        server_name 123test.hale.com;
        location / {
          proxy_pass http://123test;
          proxy_set_header X-Real-IP $remote_addr;
          proxy_cache first;
          proxy_cache_valid 200 10m;
          add_header X-Via $server_addr;
          add_header X-Cache $upstream_cache_status;
        }
     }


  另外三种常用的缓存:
     open_log_cache: 日志缓存
     open_file_cache:
     fastcgi_cache:




将特定的请求发送到特定的服务器

upstream phpser {
       server 192.168.1.10;
       server 192.168.1.11;
     }

upstream imgser {
       server 192.168.1.20;
       server 192.168.1.21;
     }
     
upstream staticser {
       server 192.168.1.30;
       server 192.168.1.31;
     }

server {
    listen 80;
    server_name test.test.com;
    
    location / {
         proxy_pass http://staticser;
     }
     
     location ~* \.php$ {
         fastcgi_pass http://phpser;
     }
     
     location ~* \.(jpg|jpeg|gif|png)$ {
         proxy_pass http://imgser;
     }
     }



rewrite: URL重写
   支持正则表达式
 
    if(condition) {
    }

  测试:
    双目测试:
    ~, !~
    =, !=
    ~*, !~*

    # if 语句尽量只在location上下文中使用
    if ($request_method = "POST") {
      }

    if ($request_uri ~* "/forum") {
      }


   
    location /images/ {
              rewrite http://192.168.1.20;
     }

   
    location / {
              root html;
              index index.html;
              rewrite "^/bbs(.*)" http://192.168.1.41/forum$1 last;
         }
    
        location / {
              root html;
              index index.html;
              rewrite "^/bbs.*" "bbs.html" last;
         }

    last : 本次重写完成后,重启下一轮检查;对重写的URL再次进行重写检查
    break : 本次重写完成后,直接执行后续操作



nginx读写分离:
  上游服务器使用httpd
  在httpd.conf中的<Directory>段中修改
     <Directory "/var/www/html">
     Dav on
     Options None
     AllowOverride None
     Order allow,deny
     Allow from all
     </Directory>

   并给予此目录apache的读写执行权限
   # setfacl  -m u:apache:wrx /var/www/html/


   在nginx上定义读写分离

   upstream 123test {
        #ip_hash;
        server 192.168.21.123:80 weight=1 max_fails=2 fail_timeout=3;
        server 192.168.21.124:80 weight=1 max_fails=2 fail_timeout=3;
        #server 127.0.0.1:8080 backup;
    }

   server {
        listen 80;
        server_name 123test.hale.com;
        location / {
          proxy_pass http://123test;
            if ($request_method = "PUT") {
              proxy_pass http://192.168.21.124:80;
             }
          proxy_set_header X-Real-IP $remote_addr;
         # proxy_cache first;
         # proxy_cache_valid 200 10m;
         # add_header X-Via $server_addr;
         # add_header X-Cache $upstream_cache_status;
        }
     }

posted @ 2016-02-01 10:20  Mr.hale  阅读(255)  评论(0编辑  收藏  举报