Nginx:根据客户端IP地址重定向后端流量

时间:2020-01-09 10:42:47  来源:igfitidea点击:

我在nginx反向代理服务器前面有四个Apache后端服务器。
如何确保nginx反向代理负载均衡器始终将特定的客户端IP地址(例如IP 1.2.3.4)请求发送到http://apachereadwrite /后端,其余部分发送到http://apachereadonly /后端?

Nginx Web服务器不支持"如果有条件"配置。
您可以根据客户端IP地址重定向和/或者选择配置。
在这种情况下,您需要使用名为$remote_addr的变量,该变量可用于检索有关用户ip地址的信息。
如果您要根据客户端IP地址或者VPN地址为自己的办公室IP地址提供文件上传功能,并向世界其他地方提供只读功能,则很有用:

File upload is disabled on 
        server {A,B,C} via php   

           +------+  +-----+  +-----+  +-----+
           |      |  |     |  |     |  |     |
           |      |  |     |  |     |  |     | 
Backends  |  A   |  |  B  |  |  C  |  |  D  | File upload enabled 
           |      |  |     |  |     |  |     | on server D via PHP
           |      |  |     |  |     |  |     |
           |      |  |     |  |     |  |     |
           +--+---+  +-+---+  +--+--+  +-+---+
              |        |         |       |
              |        |         |       |
              +--------+---------+-------+
                             |
                             |
                         +---+---+
                         |       |
                         |       |
                         |       |
                         |       |
                         |       |
                         |       |
                         +-------+
                       nginx reverse proxy server

编辑服务器{A,B,C}上的文件/etc/php.ini,执行:

# vi /etc/php.ini

对/etc/php.ini进行以下更改:

# Disallow uploading altogether this makes moving or injecting bad scripts/code onto your web server more difficult
file_uploads = Off
 
# Disallow treatment of file requests as fopen calls
allow_url_fopen = Off
allow_url_include = Off

在{A,B,C}上重新启动Apache服务器。
通过编辑php.ini并设置以下条目,确保在服务器A上启用了文件上传功能:

file_uploads = On
upload_max_filesize=2M
post_max_size=4M

Nginx语法

语法如下:

if ( $remote_addr ~* ip-address-here ) {
           proxy_pass http://YOUR-BACKEND-HERE;
}

首先设置默认proxy_pass:

## Default backend is apachereadonly ##
proxy_pass  http://apachereadonly;

检查客户端IP地址:

## If IP is 1.2.3.4 send backend to apachereadwrite ##
if ( $remote_addr ~* 1.2.3.4 ) {
    proxy_pass http://apachereadwrite;
}

例子

编辑nginx.conf文件,执行:

# vi nginx.conf

编辑/追加如下:

## apachereadonly backend ##
   upstream apachereadonly  {
     server 10.10.11.10:8011; 
     server 10.10.11.11:8011; 
     server 10.10.11.12:8011; 
     ip_hash; 
   }
   ## apachereadwrite backend ##
  upstream apachereadwrite {
     server 10.10.11.13:8011;
 
   }
 
        ## config ##
        location / {
                proxy_set_header        Accept-Encoding   "";
                proxy_set_header        Host              $http_host;
                proxy_set_header        X-Forwarded-By    $server_addr:$server_port;
                proxy_set_header        X-Forwarded-For   $remote_addr;
                proxy_set_header        X-Forwarded-Proto $scheme;
                proxy_set_header        X-Real-IP         $remote_addr;
                ## default backend
                proxy_pass  http://apachereadonly;
                ## send traffic to apachereadwrite backend if ip is 1.2.3.4 ##
                if ( $remote_addr ~* 1.2.3.4 ) {
                        proxy_pass http://apachereadwrite;
                }
                proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
        }
        ## rest of config ##

保存并关闭文件。
重新启动/重新加载nginx服务器:

# /etc/init.d/nginx reload

或者

# /usr/sbin/nginx -s reload