Search code examples
nginxsalt-project

nginx non http port redirection


Theres a server in a customer that runs a nginx, a salt master daemon from saltstack and a secret web app that does secret things.

Considerations:

  • In this scenario, theres only one ip, only one server and multiple DNS records available;
  • I have nginx running in port 80;
  • And salt master running in 6453;
  • A domain.example.com binding to that IP, exposing my nginx 80 port, that points to the secret webapp;
  • otherdomain.example.com binding to the same IP, exposing my nginx 80 port, that I want to use to proxy salt port.

That customer has a machine in other place, that does need to connect to the salt and the internet connection is provided by a secret organization and they only allow connections to port 80, no negotiation possible.

My question:

Is possible to use nginx to redirect the otherdomain.example.com 80 port to the 6453 port? I tried the following:

server {                                                                                                                                         
    listen 80;                                                                                                                               
    server_name otherdomain.example.com;                                                                                                       
    proxy_pass 127.0.0.1:6453;                                                                                                                   
}   

But that doesn't work as expected. It is possible? There's some way to do this using nginx?

The error I got from log was:

"proxy_pass" directive is not allowed here


Solution

  • proxy_pass needs to be specified within a location context, and is fundamentally a Web Thing. It only comes into play after the web headers are sent and interpreted.

    Things like what you're trying to accomplish are commonly done using HAProxy in tcp mode, although there is a tcp proxy module that also does similar things.

    However, I don't think you're going to be very successful, as ZMQ does not participate in the protocol (HTTP Host: headers) that easily allows you to tell the web requests apart from the non-web requests (that come in on the same port).

    My recommendation is to either find some way to use another port for this, a second IP address, or write a tricky TCP proxier that'll identify incoming HTTP and/or ZMQ connections and transparently forward them to the correct local port.