nginx(二) 反向代理配置详解

nginx(二)反向代理配置详解

nginx反向代理配置比如希望通过浏览器输入http://localhost 直接跳转到http://www.baidu.com 进入到百度首页

在nginx/conf.d/default.conf中配置:

server {
    listen       80 default_server;
    listen       [::]:80 default_server;
    server_name  _;
    root         /usr/share/nginx/html;

    # Load configuration files for the default server block.
    include /etc/nginx/default.d/*.conf;

    location / {
       proxy_pass http://www.baidu.com;//请求转向自定义的服务器列表 跳转到百度
       proxy_redirect default;
    }

    error_page 404 /404.html;
        location = /40x.html {
    }

    error_page 500 502 503 504 /50x.html;
        location = /50x.html {
    }

}

下面对nginx.conf文件进行说明:

  • nginx进程数,建议设置为等于CPU总核心数

  • 单个进程最大连接数,那么该服务器的最大连接数=连接数*进程数

  • 监听端口一般都为http端口:80;

  • 域名可以有多个,用空格隔开:例如 server_name www.baidu.com;

负载均衡列表基本配置:

•location / {}:对aspx后缀的进行负载均衡请求,假如我们要对所有的aspx后缀的文件进行负载均衡时,可以这样写:location ~ .*\.aspx$ {}

•proxy_pass:请求转向自定义的服务器列表,这里我们将请求都转向标识为http://cuitccol.com的负载均衡服务器列表;

 

在负载均衡服务器列表的配置中,weight是权重,可以根据机器配置定义权重(如果某台服务器的硬件配置十分好,可以处理更多的请求,那么可以 为其设置一个比较高的weight;而有一台的服务器的硬件配置比较差,那么可以将前一台的weight配置为weight=2,后一台差的配置为 weight=1)。weigth参数表示权值,权值越高被分配到的几率越大;

 

关于nginx负载均衡策略配置

# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;

# Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections  1024;
}


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

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    upstream web_pools{
      server 192.168.102.101:80 weight=1;
      server 192.168.102.102:22222 weight=3;
    }

    include /etc/nginx/conf.d/*.conf;
}
                                

 

/etc/nginx/conf.d/default.d 配置如下:

#
# The default server
#

#upstream baidu{
#   server https://www.baidu.com/;
#}

server {
    listen       80 default_server;
    listen       [::]:80 default_server;
    server_name  baidu weixin;
    root         /usr/share/nginx/html;

    # Load configuration files for the default server block.
    include /etc/nginx/default.d/*.conf;

    location / {
       proxy_pass http://web_pools;
       proxy_redirect default;
    }

    error_page 404 /404.html;
        location = /40x.html {
    }

    error_page 500 502 503 504 /50x.html;
        location = /50x.html {
    }

}

配置参数可参考(https://www.zybuluo.com/phper/note/89391)

常见配置

user root;
worker_processes  4;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
     upstream yytest {
     server 192.168.102.101:80;
}
upstream zztest {
    server 192.168.102.102:80;
}
upstream xxtest {
    server 192.168.102.103:20003;
}
    include       /etc/nginx/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  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;
    server {
        listen       80 ;
        server_name  yytest.witpos.cn;
        root         /usr/share/nginx/html;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {
                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_pass http://yytest;
        }
        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }
                server {
        listen       80 ;
        server_name  zztest.witpos.cn;
        root         /usr/share/nginx/html;
         include /etc/nginx/default.d/*.conf;

        location / {
                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_pass http://wsctest;
        }
        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
        }
    server {
        listen       80 ;
        server_name  zztest.witpos.cn;
        root         /usr/share/nginx/html;
         include /etc/nginx/default.d/*.conf;

        location / {
                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_pass http://zztest;
        }
        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
        }

}

 

关于location 正则匹配规则说明

location  = / {
  # 只匹配"/".
  [ configuration A ] 
}
location  / {
  # 匹配任何请求,因为所有请求都是以"/"开始
  # 但是更长字符匹配或者正则表达式匹配会优先匹配
  [ configuration B ] 
}
location ^~ /images/ {
  # 匹配任何以 /images/ 开始的请求,并停止匹配 其它location
  [ configuration C ] 
}
location ~* \.(gif|jpg|jpeg)$ {
  # 匹配以 gif, jpg, or jpeg结尾的请求. 
  # 但是所有 /images/ 目录的请求将由 [Configuration C]处理.   
  [ configuration D ] 
}

请求URI例子:

  • / -> 符合configuration A
  • /documents/document.html -> 符合configuration B
  • /images/1.gif -> 符合configuration C
  • /documents/1.jpg ->符合 configuration D

 

参考文章:

http://www.jianshu.com/p/bed000e1830b

http://www.jianshu.com/p/4b9e00408837

转载于:https://my.oschina.net/chenxiaobian/blog/714293

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值