DEV Community

Jordon Replogle
Jordon Replogle

Posted on

20 1

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.

Image of Timescale

🚀 pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applications—without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read full post →

Top comments (4)

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
 
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 (:

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
 
khimoc97 profile image
khimoc97 •

Sorry to digging this up. I’m trying to use haproxy as reverse proxy for my mssql but I keep get the issue about pre-login handshake. Do you have any idea I can overcome this. Many thanks

👋 Kindness is contagious

Explore a sea of insights with this enlightening post, highly esteemed within the nurturing DEV Community. Coders of all stripes are invited to participate and contribute to our shared knowledge.

Expressing gratitude with a simple "thank you" can make a big impact. Leave your thanks in the comments!

On DEV, exchanging ideas smooths our way and strengthens our community bonds. Found this useful? A quick note of thanks to the author can mean a lot.

Okay