Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8126353
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T07:07:07+00:00 2026-06-06T07:07:07+00:00

nginx is a killer static file server. it can serve node.js , as in

  • 0

nginx is a killer static file server.

it can serve node.js, as in this example, but in a limited fashion.

but nginx is apparently unable to proxy websockets.

the only thing I found that might work is using HAProxy front end as per this article – but it’s from October 6, 2011.

this has to be a common problem, but I’m not finding a very common solution.


Solution

(see https://github.com/bangkok-maco/barebone-node for complete solution and details)

ip testing schema:

  • 127.0.0.12 – http://www.chat.nit – public, in /etc/hosts and haproxy
  • 127.0.1.12 – internal nginx web server
  • 127.0.2.12 – internal chat serving node.js socket.io

/etc/haproxy/haproxy.cfg:

global
 maxconn 4096
 nbproc 2
 daemon
 # user nobody
 log             127.0.0.1       local1 notice

defaults
 mode http

# listen on 127.0.0.12:80
frontend app
 bind 127.0.0.12:80
 mode tcp
 timeout client 86400000
 default_backend www_backend
 acl is_chat hdr_dom(Host) chat
 acl is_websocket path_beg /socket.io

 use_backend chat_socket_backend if is_websocket is_chat
 tcp-request inspect-delay 500ms
 tcp-request content accept if HTTP

# ngnix on 127.0.1.12:80
backend www_backend
 balance roundrobin
 option forwardfor
 mode http
 option httplog
 option httpclose
 timeout server 30000
 timeout connect 4000
 server w1 127.0.1.12:80 weight 1 maxconn 1024 check

# node (socket.io) on 127.0.2.12:80
backend chat_socket_backend
 balance roundrobin
 mode http
 option httplog
 option forwardfor
 timeout queue 5000
 timeout server 86400000
 timeout connect 86400000
 timeout check 1s
 no option httpclose
 option http-server-close
 option forceclose
 server s14 127.0.2.12:8000 weight 1 maxconn 1024 check

/etc/nginx/sites-enabled/www.chat.nit

server {
    listen   127.0.1.12:80;

    root /data/node/chat;
    index client.html;

    server_name www.chat.nit;

    # favicon.ico is in /images
    location = /favicon.ico$ { rewrite /(.*) /images/$1 last; }

    # standard includes
    location ^~ /(css|images|scripts)/ {
            try_files $uri =404;
    }

    # html page (only in root dir)
    location ~ ^/([-_a-z]+).html$ {
            try_files $uri =404;
    }

    error_page 404 /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
            root /usr/share/nginx/www;
    }
}

chat (node.js): server.js

var app = require('http').createServer()
   , io = require('socket.io').listen(app);    
app.listen(8000,'127.0.2.12');

io.sockets.on('connection', function(socket) {
  ...
};

chat: client.html

<head>
  <script src="/scripts/socket.io/socket.io.js"></script>
  <script>
    var socket = io.connect('http://www.chat.nit:80'); 
    ...
  </script>
</head>

notes:

  1. link socket.io client js into scripts/ directory

    /…/scripts$ ln -s ../node_modules/socket.io/node_modules/socket.io-client/dist/ socket.io

  2. /etc/default/haproxy (contrary to text, must set to work at all)

    ENABLED=1

  3. this version haproxy not logging. found kvz’s write up on how to use rsyslogd via 127.0.0.1, but could not make it fly.

  4. this solution is working – not sysadmin quality to be sure. (enhancements more than welcome.)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T07:07:10+00:00Added an answer on June 6, 2026 at 7:07 am

    Here’s my (old and for testing purposes) HAProxy config for proxying WebSockets and normal HTTP requests.

    global
        maxconn 4096
        nbproc 2
        daemon
        user nobody
    
    defaults
        mode http
    
    frontend app
        bind 0.0.0.0:8000
        mode tcp
        timeout client 86400000
        default_backend www_backend
        acl is_websocket path_beg /sockets
    
        use_backend socket_backend if is_websocket
        tcp-request inspect-delay 500ms
        tcp-request content accept if HTTP
    
    backend www_backend
        balance roundrobin
        option forwardfor
        mode http
        option httplog
        option httpclose
        timeout server 30000
        timeout connect 4000
        server w1 localhost:8080 weight 1 maxconn 1024 check
    
    backend socket_backend
        balance roundrobin
        mode http
        option httplog
        option forwardfor
        timeout queue 5000
        timeout server 86400000
        timeout connect 86400000
        timeout check 1s
        no option httpclose
        option http-server-close
        option forceclose
        server s1 localhost:8081 weight 1 maxconn 1024 check
    

    Note that I am recognizing whether request is WS or not by looking at path (acl is_websocket path_beg /sockets line). This can be replaced with for example this:

    acl is_websocket hdr(Upgrade) -i WebSocket
    

    or this:

    acl is_websocket hdr_beg(Host) -i ws
    

    or both. Proxying to nginx with this config should work out of the box.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to nginx to serve a static file from website root (
I have this nginx vhost file server { # php/fastcgi listen 80; server_name trinityplex.com
I am setting up nginx as a sort of static file server. For some
To my knowledge Nginx can only password protect directories from within the configuration file(s).
In my nginx.conf I have this line in my server {} section: log_format debug
nginx is handling all static page requests, but from within my fastcgi application I
when server (apache or nginx) logging http request into the log file, (i.e. append
I have running nginx on my server ansel.ms and a node.js app on ansel.ms:46156.
I am using nginx as a frontend to an apache server. The config file
my nginx server is actually proxying my node backend (which listens on port 3000)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.