負載均衡

#可以將long里面的所有代碼刪除,復制粘貼一下內(nèi)容即可生效負載均衡
#這個寫在server模塊上面
upstream a.com {
  server  192.168.1.127:80;  #有多少個服務器就添加多少個ip
  server  192.168.1.127:8080;  #有多少個服務器就添加多少個ip
}
server {
        listen 80 ;
        server_name long;
# localtion里面
        location / {
        proxy_pass         http://a.com;   #這個地址一定是上面定義的負載均衡的名字
        proxy_set_header   Host             $host;
        proxy_set_header   X-Real-IP        $remote_addr;
        proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
        }
   }

也可以加上以下兩種輪訓方式

1.weight
指定輪詢幾率,weight和訪問比率成正比,用于后端服務器性能不均的情況。
upstream a.com {
    server 192.168.1.127:80 weight=5;
    server 192.168.1.127:8080 weight=10;
}
2.ip_hash
每個請求按訪問ip的hash結果分配,這樣每個訪客固定訪問一個后端服務器,可以解決session的問題。
upstream a.com {
                  ip_hash;   //實現(xiàn)輪詢 ,一個電腦再次訪問一個網(wǎng)站時,所訪問的服務器不變
                  192.168.1.127:80 weight=2; //weight是權重默認是1數(shù)值越大,被訪問到的概率越大。(加weight的時候,必須去掉ip_hash)   //自己的網(wǎng)站的IP
                  192.168.1.127:8080;  //自己網(wǎng)站的IP
            }
  • 注意,server后面只能寫ip,不可以寫站點名
  • 我們也可以只在一個服務器上做測試,只需要復制下site-avilable/default并改稱不沖突的端口號即可。我這里改成了8080端口

upstream模塊:upstream指令主要是用于設置一組可以在proxy_pass和fastcgi_pass指令中使用代理服務器,默認負載均衡方式為輪詢 ip_hash(輪詢)和weight(權重)是互斥的,只可以選擇使用一種方式

動靜分離的配置

#可以將long里面的所有代碼刪除,復制粘貼一下內(nèi)容即可生效負載均衡
upstream static {
server  192.168.1.127:80;  #這個對應default站點
}
upstream php {
server  192.168.1.127:8080;  #這個對應jin站點,我們要在
}
server {
      listen 80 ;
      server_name long;
index index.html index.php index.htm index.nginx-debian.html;
# localtion里面

      location / {
#此處我不知道為什么這樣寫,我想靜態(tài)的我們一般都用html
# 根據(jù)匹配php的寫法,我改成   location  ~\.html${  測試發(fā)現(xiàn)也是可以的
      proxy_pass         http://static;   #這個地址一定是上面定義的負載均衡的名字
      proxy_set_header   Host             $host;
      proxy_set_header   X-Real-IP        $remote_addr;
      proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
      }
         location ~ \.php$ { #匹配php文件用這個代理
        proxy_pass         http://php;   #這個地址一定是上面定義的負載均衡的名字
        proxy_set_header   Host             $host;
        proxy_set_header   X-Real-IP        $remote_addr;
        proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
    }
 }

# 上面的upstream內(nèi)容,嚴格來說不應該寫在`/etc/nginx/site-avilible/long`里面,
我們應該寫在`cd /etc/nginx/con.d`文件夾里。這個文件夾是個個空文件夾
`vim upstream.conf`(注意,此處名字可以隨便寫,但必須以.conf為后綴)

upstream static {
server  192.168.1.127:80;  #這個對應default站點
}
upstream php {
server  192.168.1.127:8080;  #這個對應jin站點,我們要在
}

實驗。我們在fefault對應的站點下寫index.html,test.html,在jin對應的站點下面寫index.php,test.php;此時我們訪問http://long/index.html,http://index.php,得到的結果分別就是我們相應站點下面的內(nèi)容



user  nginx nginx;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    use epoll;
    worker_connections  1024;
}


http {
    include       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  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;
    #Tomcat的服務器集群
    upstream up {
    server 101.1.1.1 weight=5 max_fails=2 fail_timeout=30;  
    server 101.2.2.2 weight=5 max_fails=2 fail_timeout=30 down;
    }

    server {
        listen       80;
        server_name  123.com  test.com;
        root            /usr/local;

        if ($host = test.com) {
            return   301 https://$host$request_uri;
        }

          #java服務器提供api的公共服務
        location ^~ /test {
          proxy_pass http://up/test;
          proxy_cookie_path /up/ /;
          
          proxy_redirect off;
          proxy_set_header Host $host;
          proxy_set_header X-Real-IP $remote_addr;
          proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
          proxy_connect_timeout   10;
          proxy_send_timeout      30;
          proxy_read_timeout      90;
        }
    }


    # HTTPS server
    #
    server {
         listen       443 ssl;
         server_name  kjzb.com;

         ssl_certificate      /usr/local/nginx/conf/kjzb_com.crt;
         ssl_certificate_key  /usr/local/nginx/conf/kjzb_com.key;


    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

        location / {
                    root   html/icp;
                #如果是手機移動端訪問內(nèi)容
                   if ( $http_user_agent ~ "(MIDP)|(WAP)|(UP.Browser)|(Smartphone)|(Obigo)|(Mobile)|(AU.Browser)|(wxd.Mms)|(WxdB.Browser)|(CLDC)|(UP.Link)|(KM.Browser)|(UCWEB)|(SEMC-Browser)|(Mini)|(Symbian)|(Palm)|(Nokia)|(Panasonic)|(MOT-)|(SonyEricsson)|(NEC-)|(Alcatel)|(Ericsson)|(BENQ)|(BenQ)|(Amoisonic)|(Amoi-)|(Capitel)|(PHILIPS)|(SAMSUNG)|(Lenovo)|(Mitsu)|(Motorola)|(SHARP)|(WAPPER)|(LG-)|(LG/)|(EG900)|(CECT)|(Compal)|(kejian)|(Bird)|(BIRD)|(G900/V1.0)|(Arima)|(CTL)|(TDG)|(Daxian)|(DAXIAN)|(DBTEL)|(Eastcom)|(EASTCOM)|(PANTECH)|(Dopod)|(Haier)|(HAIER)|(KONKA)|(KEJIAN)|(LENOVO)|(Soutec)|(SOUTEC)|(SAGEM)|(SEC-)|(SED-)|(EMOL-)|(INNO55)|(ZTE)|(iPhone)|(Android)|(Windows CE)|(Wget)|(Java)|(curl)|(Opera)" )
                           {
                                      root html/m.icp;
                           }     
                index  index.html index.htm;
                
          }
        location ~ \.php$ {
               root       html/icp;
             if ( $http_user_agent ~ "(MIDP)|(WAP)|(UP.Browser)|(Smartphone)|(Obigo)|(Mobile)|(AU.Browser)|(wxd.Mms)|(WxdB.Browser)|(CLDC)|(UP.Link)|(KM.Browser)|(UCWEB)|(SEMC-Browser)|(Mini)|(Symbian)|(Palm)|(Nokia)|(Panasonic)|(MOT-)|(SonyEricsson)|(NEC-)|(Alcatel)|(Ericsson)|(BENQ)|(BenQ)|(Amoisonic)|(Amoi-)|(Capitel)|(PHILIPS)|(SAMSUNG)|(Lenovo)|(Mitsu)|(Motorola)|(SHARP)|(WAPPER)|(LG-)|(LG/)|(EG900)|(CECT)|(Compal)|(kejian)|(Bird)|(BIRD)|(G900/V1.0)|(Arima)|(CTL)|(TDG)|(Daxian)|(DAXIAN)|(DBTEL)|(Eastcom)|(EASTCOM)|(PANTECH)|(Dopod)|(Haier)|(HAIER)|(KONKA)|(KEJIAN)|(LENOVO)|(Soutec)|(SOUTEC)|(SAGEM)|(SEC-)|(SED-)|(EMOL-)|(INNO55)|(ZTE)|(iPhone)|(Android)|(Windows CE)|(Wget)|(Java)|(curl)|(Opera)" )
                {
                                root html/m.icp;
                }     
                fastcgi_pass   127.0.0.1:9000; 
                fastcgi_index  index.php;
                fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;                                                               fastcgi_param HTTPS on;
                include        fastcgi_params;
        }
 }
}

nginx實現(xiàn)屏蔽IP

手冊-Module ngx_stream_access_module

隨便寫一個 .conf結尾的配置文件, 比如我們此處用 ip.conf文件

ip.conf 文件
# 屏蔽127.0.0.1
 deny 127.0.0.1; 
# 屏蔽所有ip
deny all;
# 允許127.0.0.1,192.168.1.1
 allow 127.0.0.1; 
 allow 192.168.1.1;
# 允許所有ip進入
allow all;
#屏蔽整個段即從123.0.0.1到123.255.255.254訪問的命令
deny 123.0.0.0/8
#屏蔽IP段即從123.45.0.1到123.45.255.254訪問的命令
deny 123.45.6.0/24


 然后進入配置文件的`http`模塊,或者`server`文件,或者`server`里面的`localtion`匹配模塊都可以
但是我們大多的需求其實是在 server里面
最后編輯于
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內(nèi)容