Question

I have Nginx as my front-end web server listening on port 80. And certain requests, I've set up nginx to reverse proxy it to a mochiweb based web server that I've written, listening on Port 8000. My nginx configuration for this looks like this:

location /mymochiserver {

            proxy_pass         http://127.0.0.1:8000;
            proxy_redirect     off;

            proxy_set_header   Host             $host;
            proxy_set_header   X-Real-IP        $remote_addr;
            proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;

            client_max_body_size       10m;
            client_body_buffer_size    128k;

            proxy_connect_timeout      90;
            proxy_send_timeout         90;
            proxy_read_timeout         90;

            proxy_buffer_size          4k;
            proxy_buffers              4 32k;
            proxy_busy_buffers_size    64k;
            proxy_temp_file_write_size 64k;
        }

Now, when I access the URL http://localhost/mymochiserver I don't see a response on the browser. The browser just says "Waiting for localhost". mymochiserver prints some trace to the terminal window from which it is run, whenever a user connects to it, and right now, I do see the trace for each browser window I open to connect this URL. But I don't see any of the output I'm expecting to see being written to the browser. BUT, when I directly access the URL http://127.0.0.1:8000/ everything works fine, and I see the output from mymochiserver on the browser. So it works when directly called. But when reverse-proxied through nginx, it doesn't seem to be working. Any idea what could be wrong?


Update: In my Mochiweb application I have these lines of code:

Socket = Req:get(socket),
inet:setopts(Socket, [{active, once}]),
proc_lib:hibernate(?MODULE, feed, [Response, Userid, 1]);

It is basically a COMET application where users will connect to the mymochiserver and the server pushes out data to all the connected clients. If there is no data to be sent from the server, I hibernate the process. And then when woken up, I call the feed function to send out the data. And if I remove the hibernation code, everything works well, and I see output in the browser. But if I do hibernate, it doesn't work. Any idea what's going wrong?

Was it helpful?

Solution

Fixed!

Reference: http://timanovsky.wordpress.com/2009/01/09/toward-a-million-user-long-poll-http-application-nginx-erlang-mochiweb/

I had to turn off proxy buffering and increase the proxy_read_timeout in nginx to make it work. So my config file looks like this now:

location /mymochiapp {
            proxy_pass         http://127.0.0.1:8000;
            proxy_redirect     off;

            proxy_set_header   Host             $host;
            proxy_set_header   X-Real-IP        $remote_addr;
            proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;

            client_max_body_size       10m;
            client_body_buffer_size    128k;

            proxy_connect_timeout      90;
            proxy_send_timeout         90;
            proxy_read_timeout         3600;

            proxy_buffering off;
        }

Thank you thomas55 for pointing out the answer!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top