zoukankan      html  css  js  c++  java
  • 一个nginx*, 负载均衡的例子

    #/etc/nginx/conf.d/master.conf
    #区分大小写
    
    #设定负载均衡的服务器列表
    upstream master.balancing {
        #weigth参数表示权值,权值越高被分配到的几率越大
        server 10.1.9.55:81 weight=4;
        server 10.1.9.50:8983 weight=1;
    }
    
    server {
        listen       80;
        #定义使用www.xx.com访问
        server_name  master.jobstreet.com;
        #access_log /var/log/master_access_log main;
        #error_log  /var/log/master_error_log  debug_http;
        client_max_body_size 60M;
        client_body_buffer_size 512k;
    
        location / {
            #定义首页索引文件的名称
            index Favorites.htm
            port_in_redirect on;
            #请求转向mysvr 定义的服务器列表
            proxy_pass     http://master.balancing;
            proxy_redirect  off;
            #后端的Web服务器可以通过X-Forwarded-For获取用户真实IP
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
        
        # 定义错误提示页面
        error_page   500 502 503 504 /50x.html;  
        location = /50x.html {
            root   /root;
        }
        
        #静态文件,nginx自己处理
        #~ 为区分大小写匹配 
        #~* 为不区分大小写匹配 
    #    location ~ ^/(images|javascript|js|css|flash|media|static)/{
        location ~* .*.(html|htm|gif|jpg|jpeg|bmp|png|ico|txt|js|css)$ {
            #文件和目录不存在的时重定向
            #if (!-e $request_filename){
                #proxy_pass http://127.0.0.1; #这里是跳转到代理ip,这个代理ip上有一个监听的web服务器
                #rewrite ^/ http://www.jobsdb.com/none.html;  #跳转到这个网页去
                #return 404; #直接返回404码,然后会寻找root指定的404.html文件
            #}
            
            root   /var/www/html/static;
            #过期30天,静态文件不怎么更新,过期可以设大一点,如果频繁更新,则可以设置得小一点。
            expires  7d;
        }
        
        #PHP 脚本请求全部转发到 FastCGI处理. 使用FastCGI默认配置.
        location ~ .php$ {
            root /root;
            fastcgi_pass 127.0.0.1:9000;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME /home/www/www$fastcgi_script_name;
            include fastcgi_params;
        }
        
        #禁止访问 .htxxx 文件
        location ~ /.ht {
            deny all;
        }
        
        #禁止以/data开头的文件,可以禁止/data/下多级目录下.log.txt等请求
        location ~ ^/data { 
            deny all; 
        } 
    }

    大小写匹配

    ~ 为区分大小写匹配 

    ~* 为不区分大小写匹配 

    !~和!~*分别为区分大小写不匹配及不区分大小写不匹配 

    文件及目录匹配

    -f和!-f用来判断是否存在文件 

    -d和!-d用来判断是否存在目录 

    -e和!-e用来判断是否存在文件或目录 

    -x和!-x用来判断文件是否可执行 

    flag标记

    last 相当于Apache里的[L]标记,表示完成rewrite

    break 终止匹配, 不再匹配后面的规则。

    redirect 返回302临时重定向 地址栏会显示跳转后的地址。

    permanent 返回301永久重定向 地址栏会显示跳转后的地址。

    logcation的几个使用实例:

       1)location  / { }:匹配任何查询,因为所有请求都以 / 开头。但是正则表达式规则将被优先和查询匹配。
       2)location =/ {}:仅仅匹配/
       3)location ~* .(gif|jpg|jpeg)$

         {
            rewrite .(gif|jpg)$ /logo.png;
         }:location不区分大小写,匹配任何以gif,jpg,jpeg结尾的文件。

    几个实例:

    多目录转成参数 

    要求:abc.domian.com/sort/2 => abc.domian.com/index.php?act=sort&name=abc&id=2 

    规则配置:

    if ($host ~* (.*).domain.com) { 

        set $sub_name $1;

        rewrite ^/sort/(d+)/?$ /index.php?act=sort&cid=$sub_name&id=$1 last; 

    目录对换 

    要求:/123456/xxxx -> /xxxx?id=123456 

    规则配置:

    rewrite ^/(d+)/(.+)/ /$2?id=$1 last; 

    再来一个针对浏览器优化的自动rewrite,这里rewrite后的目录可以是存在的;

    例如设定nginx在用户使用ie的使用重定向到/nginx-ie目录

    规则如下:

     if ($http_user_agent ~ MSIE) {

         rewrite ^(.*)$ /nginx-ie/$1 break; 

    目录自动加“/” ,这个功能一般浏览器自动完成

    if (-d $request_filename){ 

    rewrite ^/(.*)([^/])$ http://$host/$1$2/ permanent; 

  • 相关阅读:
    Angular 组件通信的三种方式
    Vue 之keep-alive的使用,实现页面缓存
    Angular Service设计理念及使用
    Git提交规范
    angular的生命周期
    CPU 是如何认识和执行代码的
    Ubuntu 常用软件
    UltraSQL / sqlserver-kit (SQLServer DBA )高手
    便宜的网站模板下载
    Audio over Bluetooth: most detailed information about profiles, codecs, and devices
  • 原文地址:https://www.cnblogs.com/dfg727/p/5522391.html
Copyright © 2011-2022 走看看