DEV Community

Jordon Replogle
Jordon Replogle

Posted on

Reverse Proxy MS SQL with Nginx

Hey it's my first post! Did something nifty today to work around a technical issue I ran into while trying to do my job.

With the COVID-19 thing, I am working from home. The VPN connection we have is a point to point, so I have access to the office LAN. But I don't have access to our data center that is connected to our office via VPN.

To get around this today I had a Linux VM spun up to create a reverse proxy. I first started with HAProxy, and it worked great... till I needed UDP for port 1434. So I switched to Nginx, and was able to proxy both 1433/TCP and 1434/UDP to give me access to the MS SQL server.

Here's the simple Nginx config:

stream {
    upstream dbtcp {
        server db1:1433;
    }

    upstream dbudp {
        server db1:1434;
    }

    server {
        listen 1433;
        proxy_pass dbtcp;
        proxy_connect_timeout 1s; # detect failure quickly
    }

    server {
        listen 1434 udp;
        proxy_pass dbudp;
        proxy_connect_timeout 1s; # detect failure quickly
    }
}
Enter fullscreen mode Exit fullscreen mode

Note that Nginx listens to tcp by default and you only need to denote udp.

Discussion (3)

Collapse
andyhammer profile image
AndyHammer

Jordan,
This is awesome. There is so little out there about connecting to MS SQL through a proxy. We are trying to do the same thing with Nginx but cannot figure out how to connect. Tools like SQL Server Management Studio and SQLCMD do not seem to have an option for setting a proxy server. How are you connecting?

Collapse
disgustedotter profile image
DisgustedOtter

Thank you for this, it worked like a charm. I have a question, is it possible to have multiple streams to connect to more than 1 db server?

Collapse
damazy9 profile image
Damazy

...and You should mention, that only named instance need 1434 UDP port forwarding... with default instance it's only TCP 1433 (: