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;
        }
     }php

相關文章
相關標籤/搜索