Sql-Server

在 IP 範圍上啟用防火牆的代理 Azure SQL 數據庫

  • April 28, 2020

我有以下設置

  • 啟用了防火牆的 Azure SQL 數據庫,因此它只允許來自 104.xxx IP 地址的連接
  • Azure 託管 Linux VM,與 Azure DB 位於同一位置,外部 IP 為 104.xxx
  • 本地 Linux 虛擬機,可以通過 vnet 連接 Azure 虛擬機,外部 IP 地址為 91.xxx

目標是能夠使用 Azure 託管的 VM 作為代理將本地 VM 連接到 SQL DB。

我嘗試以兩種方式設置它,使用 Nginx + RTMP Streaming Module 描述here Using a TCP proxy to connect to SQL Database over VPN,如下nginx.conf

events {
 worker_connections  1024;
}
stream {
 upstream sqlvm {
   server sqldb.database.windows.net:1433;
 }
 server {
   listen 1433;
   proxy_pass sqlvm;
 }
}

tcp第二種方式是在模式下使用 HAProxy 。

listen sqldb
   bind *:1433 
   mode tcp
   server sqldb sqldb.database.windows.net:1433 check port 1433 inter 1000

在這兩種情況下,與 SQL 客戶端(例如sqlcmd)的連接都以同樣的方式失敗。

# 10.x.x.x is the IP of Azure VM in this setup
$ sqlcmd -S 10.x.x.x,1433 -d dbName -U user@sqldb.database.windows.net -P password
Sqlcmd: Error: Microsoft ODBC Driver 17 for SQL Server : Cannot open server 'sqldb' requested by the login. Client with IP address '91.x.x.x' is not allowed to access the server.  To enable access, use the Windows Azure Management Portal or run sp_set_firewall_rule on the master database to create a firewall rule for this IP address or address range.  It may take up to five minutes for this change to take effect..

在代理 VM 上執行時,相同的命令有效。

所以不知何故,Azure SQL 知道原始客戶端 IP 地址並阻止它,即使它正在通過代理。

鑑於我們在 TCP 級別上代理,這怎麼可能?此設置是否有解決方法/工作配置?

似乎這個問題的答案在Azure SQL Connectivity Architecture的文件中。

連接策略

預設值:這是創建後在所有伺服器上生效的連接策略,除非您將連接策略顯式更改為代理或重定向。對於源自 Azure 內部的所有客戶端連接(例如,來自 Azure 虛擬機)的預設策略是重定向,對於源自外部的所有客戶端連接(例如,來自本地工作站的連接),預設策略是代理。

意味著第一個連接通過代理,但在重定向後它完全繞過代理。

在上述設置的情況下,解決方案是將代理 VM 移到 Azure 之外。也可以通過將數據庫連接模式更改為代理模式來完成(但以換取性能)

引用自:https://serverfault.com/questions/1014453