Nginx 反向代理到自定义 Mochiweb 应用程序

发布于 2024-07-23 23:48:19 字数 1729 浏览 7 评论 0原文

我有 Nginx 作为我的前端 Web 服务器,监听端口 80。对于某些请求,我设置了 nginx 将其反向代理到我编写的基于 mochiweb 的 Web 服务器,监听端口 8000。我的 nginx 配置为看起来像这样:

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;
        }

现在,当我访问 URL http://localhost/mymochiserver 时,我没有看到响应在浏览器上。 浏览器只是显示“正在等待本地主机”。 每当用户连接到 mymochiserver 时,mymochiserver 都会将一些跟踪打印到运行它的终端窗口,现在,我确实看到了为连接此 URL 而打开的每个浏览器窗口的跟踪。 但我没有看到任何我期望看到的输出被写入浏览器。 但是,当我直接访问 URL http://127.0.0.1:8000/ 时,一切正常,并且我在浏览器上看到 mymochiserver 的输出。 所以直接调用就可以了。 但是当通过 nginx 进行反向代理时,它似乎不起作用。 知道可能出什么问题吗?


更新: 在我的 Mochiweb 应用程序中,我有以下几行代码:

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

它基本上是一个 COMET 应用程序,用户将连接到 mymochiserver,服务器将数据推送到所有连接的客户端。 如果没有数据要从服务器发送,我会休眠该进程。 然后当我醒来时,我调用 feed 函数来发送数据。 如果我删除休眠代码,一切都会正常,并且我会在浏览器中看到输出。 但如果我冬眠的话,它就不起作用了。 知道出了什么问题吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

泪冰清 2024-07-30 23:48:19

固定的!

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

我不得不关闭代理缓冲并增加nginx中的proxy_read_timeout以使其工作。 所以我的配置文件现在看起来像这样:

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;
        }

谢谢 thomas55 指出答案!

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文