阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

Nginx反向代理+负载均衡简单实现(http方式)

185次阅读
没有评论

共计 15179 个字符,预计需要花费 38 分钟才能阅读完成。

1)nginx 的反向代理:proxy_pass
2)nginx 的负载均衡:upstream

下面是 nginx 的反向代理和负载均衡的实例:

负载机:A 机器:103.110.186.8/192.168.1.8
后端机器 1:B 机器:192.168.1.102
后端机器 2:C 机器:192.168.1.103

需求:
1)访问 A 机器的 8080 端口,反向代理到 B 机器的 8080 端口;
访问 A 机器的 8088 端口,反向代理到 C 机器的 8088 端口;
访问http://103.110.86.8:8090/ios,反向代理到 B 机器http://192.168.1.102:8090/ios/

2)访问 A 机器的 80 端口,负载均衡到后端的两台机器 B 和 C 的 80 端口

操作记录:
————————————————————————————–
负载机:A 机器上的操作记录:
1)编译安装 nginx
[root@opd ~]# yum install -y pcre* openssl* gcc gcc+
[root@opd ~]# cd /opt/src
[root@src ~]# wget http://nginx.org/download/nginx-1.8.0.tar.gz
[root@src ~]# tar -zxvf nginx-1.8.0.tar.gz
[root@src ~]# cd nginx-1.8.0
# 添加 www 用户,其中 - M 参数表示不添加用户家目录,- s 参数表示指定 shell 类型

[root@nginx-1.8.0 ~]#useradd www -M -s /sbin/nologin
[root@nginx-1.8.0 ~]#vim auto/cc/gcc
# 将这句注释掉 取消 Debug 编译模式 大概在 179 行
#CFLAGS=”$CFLAGS -g”

# 我们再配置下 nginx 编译参数
[root@nginx-1.8.0 ~]# ./configure –prefix=/opt/nginx –user=www –group=www –with-http_stub_status_module –with-http_ssl_module
[root@nginx-1.8.0 ~]#make
[root@nginx-1.8.0 ~]#make install clean

2)配置 nginx
[root@nginx-1.8.0 ~]# cd /opt/nginx/conf
[root@nginx-1.8.0 conf]# vim nginx.conf         // 这个可以作为 nginx 安装后的配置规范

http {
    include      mime.types;
    default_type  application/octet-stream;
    charset utf-8;
 
    log_format  main  ‘$http_x_forwarded_for $remote_addr $remote_user [$time_local] “$request” ‘
                      ‘$status $body_bytes_sent “$http_referer” ‘
                      ‘”$http_user_agent” “$http_cookie” $host $request_time’;
    sendfile      on;
    tcp_nopush    on;
    tcp_nodelay    on;
    keepalive_timeout  65;
 
 
    fastcgi_connect_timeout 3000;
    fastcgi_send_timeout 3000;
    fastcgi_read_timeout 3000;
    fastcgi_buffer_size 256k;
    fastcgi_buffers 8 256k;
    fastcgi_busy_buffers_size 256k;
    fastcgi_temp_file_write_size 256k;
    fastcgi_intercept_errors on;
 
     
    client_header_timeout 600s;
    client_body_timeout 600s;
 
    client_max_body_size 100m;           
    client_body_buffer_size 256k;         
 
    gzip  on;
    gzip_min_length  1k;
    gzip_buffers    4 16k;
    gzip_http_version 1.1;
    gzip_comp_level 9;
    gzip_types      text/plain application/x-Javascript text/css application/xml text/javascript application/x-httpd-php;
    gzip_vary on;
 
 
    include vhosts/*.conf;
}

[root@nginx-1.8.0 conf]# ulimit -n 65535
[root@nginx-1.8.0 conf]# mkdir vhosts
[root@nginx-1.8.0 conf]# cd vhosts

配置反向代理和负载均衡
[root@nginx-1.8.0 vhosts]# vim 8080.conf

server {
    listen 8080;
    server_name localhost;
    index index.html index.php index.htm;
    root /var/www/html;
  
    access_log  /usr/local/nginx/logs/8080-access.log main;
    error_log  /usr/local/nginx/logs/8080-error.log;
 
location / {
    proxy_pass http://192.168.1.102:8080;
    proxy_redirect off ;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header REMOTE-HOST $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_connect_timeout 300;            # 跟后端服务器连接超时时间,发起握手等候响应时间
    proxy_send_timeout 300;                # 后端服务器回传时间,就是在规定时间内后端服务器必须传完所有数据
    proxy_read_timeout 600;                # 连接成功后等待后端服务器的响应时间,已经进入后端的排队之中等候处理
    proxy_buffer_size 256k;                # 代理请求缓冲区, 会保存用户的头信息以供 nginx 进行处理
    proxy_buffers 4 256k;                  # 同上,告诉 nginx 保存单个用几个 buffer 最大用多少空间
    proxy_busy_buffers_size 256k;          # 如果系统很忙时候可以申请最大的 proxy_buffers
    proxy_temp_file_write_size 256k;      #proxy 缓存临时文件的大小
    proxy_next_upstream error timeout invalid_header http_500 http_503 http_404;
    proxy_max_temp_file_size 128m;
}
}

[root@nginx-1.8.0 vhosts]# cat 8088.conf

server {
    listen 8088;
    server_name localhost;
    index index.html index.php index.htm;
    root /var/www/html;
  
    access_log  /usr/local/nginx/logs/8088-access.log main;
    error_log  /usr/local/nginx/logs/8088-error.log;
 
location / {
    proxy_pass http://192.168.1.103:8088;
    proxy_redirect off ;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header REMOTE-HOST $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_connect_timeout 300;           
    proxy_send_timeout 300;             
    proxy_read_timeout 600;             
    proxy_buffer_size 256k;               
    proxy_buffers 4 256k;                 
    proxy_busy_buffers_size 256k;       
    proxy_temp_file_write_size 256k;     
    proxy_next_upstream error timeout invalid_header http_500 http_503 http_404;
    proxy_max_temp_file_size 128m;
}
}

—————————————————————————————————————–
下面这个匹配 path 的代理设置需要注意几点:
首先一定要保证目标 B 机器,也就是 192.168.1.102 的 8090 端口站点目录下有这个匹配 path 的目录 ios 存在!!
也就是要保证 A 机器本机能顺利访问到目标 B 机器的 8090 端口的 ios 路径,即:
[root@nginx-1.8.0 vhosts]# curl http://192.168.1.102:8090/ios/ #一定要保证这个能从 A 机器访问成功!

下面几种配置都是可以的:

第一种:
[root@nginx-1.8.0 vhosts]# cat 8090.conf

server {
    listen 8090;
    server_name localhost;
    index index.html index.php index.htm;
    root /var/www/html;
  
    access_log  /usr/local/nginx/logs/8090-access.log main;
    error_log  /usr/local/nginx/logs/8090-error.log;
 
    location /ios/ {# 这种情况,这里一定要匹配的是 /ios/,不能是 /ios
    proxy_pass http://192.168.1.102:8090;      # 一定要保证 192.168.1.102 机器 8090 端口站点目录下有 ios 目录!否则访问会报错 404!
    proxy_redirect off ;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header REMOTE-HOST $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_connect_timeout 300;           
    proxy_send_timeout 300;             
    proxy_read_timeout 600;             
    proxy_buffer_size 256k;               
    proxy_buffers 4 256k;                 
    proxy_busy_buffers_size 256k;       
    proxy_temp_file_write_size 256k;     
    proxy_next_upstream error timeout invalid_header http_500 http_503 http_404;
    proxy_max_temp_file_size 128m;
}
}

第二种:
[root@nginx-1.8.0 vhosts]# cat 8090.conf

server {
    listen 8090;
    server_name localhost;
    index index.html index.php index.htm;
    root /var/www/html;
  
    access_log  /usr/local/nginx/logs/8090-access.log main;
    error_log  /usr/local/nginx/logs/8090-error.log;
 
    location /ios/ {
    proxy_pass http://192.168.1.102:8090/ios/;
    proxy_redirect off ;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header REMOTE-HOST $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_connect_timeout 300;           
    proxy_send_timeout 300;             
    proxy_read_timeout 600;             
    proxy_buffer_size 256k;               
    proxy_buffers 4 256k;                 
    proxy_busy_buffers_size 256k;       
    proxy_temp_file_write_size 256k;     
    proxy_next_upstream error timeout invalid_header http_500 http_503 http_404;
    proxy_max_temp_file_size 128m;
}
}

第三种:
[root@nginx-1.8.0 vhosts]# cat 8090.conf

server {
    listen 8090;
    server_name localhost;
    index index.html index.php index.htm;
    root /var/www/html;
  
    access_log  /usr/local/nginx/logs/8090-access.log main;
    error_log  /usr/local/nginx/logs/8090-error.log;
 
    location /ios {
    proxy_pass http://192.168.1.102:8090/ios/;        这种情况,这里一定要匹配的是/ios/,不能是/ios
    proxy_redirect off ;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header REMOTE-HOST $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_connect_timeout 300;           
    proxy_send_timeout 300;             
    proxy_read_timeout 600;             
    proxy_buffer_size 256k;               
    proxy_buffers 4 256k;                 
    proxy_busy_buffers_size 256k;       
    proxy_temp_file_write_size 256k;     
    proxy_next_upstream error timeout invalid_header http_500 http_503 http_404;
    proxy_max_temp_file_size 128m;
}
}

以上三种配置方法都保证了访问 http://103.110.86.8:8090/ios 会自动变为 http://103.10.86.8:8090/ios/,并代理到 http://192.168.1.102:8090/ios/ 的结果

—————————————————————————————————————–

[root@nginx-1.8.0 vhosts]# cat LB.conf

upstream lb {
    server 192.168.1.102:80 max_fails=3 fail_timeout=30s;  #max_fails = 3 为允许失败的次数,默认值为 1
    server 192.168.1.103:80 max_fails=3 fail_timeout=30s;  #fail_timeout = 30s 当 max_fails 次失败后,暂停将请求分发到该后端服务器的时间
}
 
server {
    listen 80;
    server_name localhost;
    index index.html index.php index.htm;
    root /var/www/html;
  
    access_log  /usr/local/nginx/logs/80-access.log main;
    error_log  /usr/local/nginx/logs/80-error.log;
 
    location / {
    proxy_pass http://lb;
    proxy_redirect off ;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header REMOTE-HOST $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_connect_timeout 300;           
    proxy_send_timeout 300;             
    proxy_read_timeout 600;             
    proxy_buffer_size 256k;               
    proxy_buffers 4 256k;                 
    proxy_busy_buffers_size 256k;       
    proxy_temp_file_write_size 256k;     
    proxy_next_upstream error timeout invalid_header http_500 http_503 http_404;
    proxy_max_temp_file_size 128m;
}
}

启动 nginx
[root@nginx-1.8.0 vhosts]# /opt/nginx/sbin/nginx -t【检查配置是否正确】
nginx: the configuration file /opt/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /opt/nginx/conf/nginx.conf test is successful
[root@host-192-168-1-102 vhosts]# /opt/nginx/sbin/nginx【启动 nginx】

————————————————————————————–
后端机:B 机器上的操作记录:
1)编译安装 nginx
[root@B ~]# yum install -y pcre* openssl* gcc gcc+
[root@B ~]# cd /opt/src
[root@B ~]# wget http://nginx.org/download/nginx-1.8.0.tar.gz
[root@B ~]# tar -zxvf nginx-1.8.0.tar.gz
[root@B ~]# cd nginx-1.8.0
# 添加 www 用户,其中 - M 参数表示不添加用户家目录,- s 参数表示指定 shell 类型

[root@nginx-1.8.0 ~]#useradd www -M -s /sbin/nologin
[root@nginx-1.8.0 ~]##vim auto/cc/gcc
# 将这句注释掉 取消 Debug 编译模式 大概在 179 行
#CFLAGS=”$CFLAGS -g”

# 我们再配置下 nginx 编译参数
[root@nginx-1.8.0 ~]# ./configure –prefix=/opt/nginx –user=www –group=www –with-http_stub_status_module –with-http_ssl_module
[root@nginx-1.8.0 ~]#make
[root@nginx-1.8.0 ~]#make install clean

2)配置 nginx
[root@nginx-1.8.0 ~]# cd /opt/nginx/conf
注意,把默认的 nginx.conf 文件中的 server 区域配置注释掉,设置 vhosts 虚拟主机的配置,如下:
[root@nginx-1.8.0 conf]# vim nginx.conf

user  www;
worker_processes  8;
   
events {
    worker_connections  65535;
}
   
http {
    include      mime.types;
    default_type  application/octet-stream;
    charset utf-8;
  
    log_format  main  '$http_x_forwarded_for $remote_addr $remote_user [$time_local]"$request" '
                      '$status $body_bytes_sent"$http_referer" '
                      '"$http_user_agent" "$http_cookie" $host $request_time';
    sendfile      on;
    tcp_nopush    on;
    tcp_nodelay    on;
    keepalive_timeout  65;
  
  
    fastcgi_connect_timeout 3000;
    fastcgi_send_timeout 3000;
    fastcgi_read_timeout 3000;
    fastcgi_buffer_size 256k;
    fastcgi_buffers 8 256k;
    fastcgi_busy_buffers_size 256k;
    fastcgi_temp_file_write_size 256k;
    fastcgi_intercept_errors on;
   
      
    client_header_timeout 600s;
    client_body_timeout 600s;
   
    client_max_body_size 100m;           
    client_body_buffer_size 256k;         
   
    gzip  on;
    gzip_min_length  1k;
    gzip_buffers    4 16k;
    gzip_http_version 1.1;
    gzip_comp_level 9;
    gzip_types      text/plain application/x-javascript text/css application/xml text/javascript application/x-httpd-php;
    gzip_vary on;
   
  
    include vhosts/*.conf;
}

[root@nginx-1.8.0 conf]# ulimit -n 65535
[root@nginx-1.8.0 conf]# mkdir vhosts
[root@nginx-1.8.0 conf]# cd vhosts

[root@nginx-1.8.0 conf]# vim 8080.conf

server {
    listen 8080;
    server_name localhost;
    index index.html index.php index.htm;
  
    access_log  /usr/local/nginx/logs/8080-access.log main;
    error_log  /usr/local/nginx/logs/8080-error.log;
 
location ~ / {
    root /var/www/html/8080;
    index index.html index.php index.htm;
}
}

[root@nginx-1.8.0 conf]# vim 8090.conf

server {
    listen 8090;
    server_name localhost;
    index index.html index.php index.htm;
  
    access_log  /usr/local/nginx/logs/8090-access.log main;
    error_log  /usr/local/nginx/logs/8090-error.log;
 
location ~ / {
    root /var/www/html/8090;        # 针对上面匹配 ios 的 path 代理,要保证站点目录 /var/www/html/8080 下有 ios 目录存在
    index index.html index.php index.htm;
}
}

[root@nginx-1.8.0 conf]# vim 80.conf

server {
   listen 80;
   server_name localhost;
   index index.html index.php index.htm;
  
   access_log  /usr/local/nginx/logs/80-access.log main;
   error_log  /usr/local/nginx/logs/80-error.log;
 
location ~ / {
   root /var/www/html;
   index index.html index.php index.htm;
}
}

启动 nginx
[root@nginx-1.8.0 vhosts]# /opt/nginx/sbin/nginx -t【检查配置是否正确】
nginx: the configuration file /opt/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /opt/nginx/conf/nginx.conf test is successful
[root@host-192-168-1-102 vhosts]# /opt/nginx/sbin/nginx【启动 nginx】

————————————————————————————–
后端机:C 机器上的操作记录:
1)编译安装 nginx
[root@C ~]# yum install -y pcre* openssl* gcc gcc+
[root@C ~]# cd /opt/src
[root@C ~]# wget http://nginx.org/download/nginx-1.8.0.tar.gz
[root@C ~]# tar -zxvf nginx-1.8.0.tar.gz
[root@C ~]# cd nginx-1.8.0
# 添加 www 用户,其中 - M 参数表示不添加用户家目录,- s 参数表示指定 shell 类型

[root@nginx-1.8.0 ~]#useradd www -M -s /sbin/nologin
[root@nginx-1.8.0 ~]##vim auto/cc/gcc
# 将这句注释掉 取消 Debug 编译模式 大概在 179 行
#CFLAGS=”$CFLAGS -g”

# 我们再配置下 nginx 编译参数
[root@nginx-1.8.0 ~]# ./configure –prefix=/opt/nginx –user=www –group=www –with-http_stub_status_module –with-http_ssl_module
[root@nginx-1.8.0 ~]#make
[root@nginx-1.8.0 ~]#make install clean

2)配置 nginx
[root@nginx-1.8.0 ~]# cd /opt/nginx/conf
注意,把默认的 nginx.conf 文件中的 server 区域配置注释掉,设置 vhosts 虚拟主机的配置,如下:
[root@nginx-1.8.0 conf]# vim nginx.conf

user  www;
worker_processes  8;
   
events {
    worker_connections  65535;
}
   
http {
    include      mime.types;
    default_type  application/octet-stream;
    charset utf-8;
  
    log_format  main  '$http_x_forwarded_for $remote_addr $remote_user [$time_local]"$request" '
                      '$status $body_bytes_sent"$http_referer" '
                      '"$http_user_agent" "$http_cookie" $host $request_time';
    sendfile      on;
    tcp_nopush    on;
    tcp_nodelay    on;
    keepalive_timeout  65;
  
  
    fastcgi_connect_timeout 3000;
    fastcgi_send_timeout 3000;
    fastcgi_read_timeout 3000;
    fastcgi_buffer_size 256k;
    fastcgi_buffers 8 256k;
    fastcgi_busy_buffers_size 256k;
    fastcgi_temp_file_write_size 256k;
    fastcgi_intercept_errors on;
   
      
    client_header_timeout 600s;
    client_body_timeout 600s;
   
    client_max_body_size 100m;           
    client_body_buffer_size 256k;         
   
    gzip  on;
    gzip_min_length  1k;
    gzip_buffers    4 16k;
    gzip_http_version 1.1;
    gzip_comp_level 9;
    gzip_types      text/plain application/x-javascript text/css application/xml text/javascript application/x-httpd-php;
    gzip_vary on;
   
  
    include vhosts/*.conf;
}

[root@nginx-1.8.0 conf]# vim 80.conf

server {
    listen 80;
    server_name localhost;
    index index.html index.php index.htm;
  
    access_log  /usr/local/nginx/logs/80-access.log main;
    error_log  /usr/local/nginx/logs/80-error.log;
 
location ~ / {
    root /var/www/html/;
    index index.html index.php index.htm;
}
}

启动 nginx

[root@nginx-1.8.0 vhosts]# /opt/nginx/sbin/nginx -t【检查配置是否正确】
nginx: the configuration file /opt/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /opt/nginx/conf/nginx.conf test is successful
[root@host-192-168-1-102 vhosts]# /opt/nginx/sbin/nginx【启动 nginx】

 

到此,上面需求中的 nginx 反向代理和负载均衡就已经配置完成了!
访问 http://103.110.86.8:8080 的结果显示的就是 B 机器,即 http://192.168.1.102:8080 的结果
访问 http://103.110.86.8:8088 的结果显示的就是 C 机器,即 http://192.168.1.108:8088 的结果
访问 http://103.110.86.8:8090/ios 的结果显示的就是 B 机器,即 http://192.168.1.102:8090/ios/ 的结果

访问 http://103.110.86.8 的请求就会被负载给到后端两台机器 http://192.168.1.102 和 http://192.168.1.103

可以在 103.110.86.8 本机可以使用 curl 和 telnet 测试到目标机器是否通顺~
[root@nginx-1.8.0 vhosts]# curl http://192.168.1.102:8080
[root@nginx-1.8.0 vhosts]# telnet 192.168.1.102 8080

——————————————————————————————————————————————–
说明一下:
上面的 nginx 反向代理的需求,除了 nginx 反代配置之外,也可以使用 iptables 的 nat 转发实现。

比如:
访问 A 机器的 8080 端口,反向代理到 B 机器的 80 端口;

iptables 的 nat 转发规则设置如下:
[root@opd ~]# iptables -t nat -A PREROUTING -p tcp -m tcp –dport 8080 -j DNAT –to-destination 192.168.1.102:80
[root@opd ~]# iptables -t nat -A POSTROUTING -d 192.168.1.102 -p tcp -m tcp –sport 80 -j SNAT –to-source 192.168.1.8
[root@opd ~]# iptables -t filter -A INPUT -p tcp -m state –state NEW -m tcp –dport 8080 -j ACCEPT

[root@opd ~]# service iptables save

**************************************
需要注意的是:
要打开 A 机器的 ip 转发功能:
[root@opd ~]# echo 1 > /proc/sys/net/ipv4/ip_forward
然后后端机器 B 的 route 路由最好也设置成 192.168.1.8
**************************************

这样,访问 http://103.110.86.8:8080 的结果就是 http://192.168.1.102 的结果

本文永久更新链接地址:http://www.linuxidc.com/Linux/2017-02/140399.htm

正文完
星哥说事-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2022-01-21发表,共计15179字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中