Nginx to apache reverse proxy, instruct use of unix sockets

11,080

Solution 1

While you most likely could set Nginx to proxy redirect to a socket using unix:/path/to/socket syntax, Apache Listen directive only accepts IPv4 or IPv6, so as far as I know you can't get Apache to listen on an unix socket.

Solution 2

You need to define an upstream like this:

upstream upstream_name {
        server unix:/path/to/socket fail_timeout=0;
}

And then set proxy_pass to reference that upstream by name, i.e.,

proxy_pass http://upstream_name
Share:
11,080

Related videos on Youtube

Quintin Par
Author by

Quintin Par

Quintessential learner and programming newbie..

Updated on September 18, 2022

Comments

  • Quintin Par
    Quintin Par over 1 year

    My Nginx reverse proxy works on the same machine as the webserver(apache) as follows

    server {  server_name site.net;
        location / {
            proxy_pass http://localhost:82;
            proxy_set_header   Host             $host;
            proxy_set_header   X-Real-IP        $remote_addr;
    
        }
    }
    

    Now instead of using TCP connections to the backend apache, how can I tune it to use unix sockets?

    Edit:
    Can someone help with the full flow, instructing apache to listen on unix sockets too

    • Ben Lessani
      Ben Lessani over 11 years
      AFAIK Apache doesn't support listening on a socket ... its a web server. If performance is your reason for doing this, the difference between sockets/tcp is negligible for most web servers - as the bottleneck is whatever app is running anyway (PHP/Perl etc.)
    • Hrvoje Špoljar
      Hrvoje Špoljar over 11 years
      I assume you have too many connections which stack up so you want to move proxy from tcp stack to socket. I would suggest you to try disable keepalive; also it might help to enable TCP_TW_REUSE (net.ipv4.tcp_tw_reuse)
  • Quintin Par
    Quintin Par over 11 years
    What about the webserver? Apache in this case.
  • Quintin Par
    Quintin Par over 11 years
    Possible to give an answer with a full flow?
  • Clint Miller
    Clint Miller over 11 years
    In a brief search, I couldn't find anything in the documentation that indicated that Apache is capable of listening on a unix domain socket.
  • Maxim Dounin
    Maxim Dounin over 11 years
    There is no need to define upstream to proxy requests to a unix socket, proxy_pass http://unix:/path/to/socket:; would do the trick as well, see nginx.org/r/proxy_pass.
  • symcbean
    symcbean over 11 years
    It would be trivial to script a proxy to map the unix socket to a network socket - but you lose any performance benefit. OTOH the lo interface doesn't have nearly the same performance constraints as a physical interface, even if it does slow start, as long as you've got big congestion windows then it should run at nearly the same speed as a unix socket.
  • c2h5oh
    c2h5oh over 11 years
    Socket will still have edge on small requests where latency is essential.
  • symcbean
    symcbean over 11 years
    The why route them via a proxy?