当将Nginx用作反向代理时,QPS急剧下降

时间:2015-09-06 16:46:02

标签: nginx erlang

在节点上设置基于Erlang的Web服务器,其中有10000个Erlang进程等待处理来自客户端的请求。当我尝试对其进行性能测试时,ab -n 1000000 -c 10000 -r http://172.17.0.28:8024/已应用,需要277.220秒才能完成。

但是,当我在2个相同的Erlang Web服务器前面设置Nginx反向代理时,它只消耗大约100秒来完成总请求的1/10。这两个服务器都与前一个相同。

我的nginx.conf如下:

user              root;
worker_processes  2;

error_log  /var/log/nginx/error.log;

pid        /var/run/nginx.pid;

events {
    use epoll;
    worker_connections  10240;
}

http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    # Load config files from the /etc/nginx/conf.d directory
    # The default server is in conf.d/default.conf
    include /etc/nginx/conf.d/*.conf;


    server {
        listen 9000;

        location / {
            proxy_pass http://backend_webserver;
        }
    }

    upstream backend_webserver {
        server 172.17.0.28:8024;
        server 172.17.0.30:8024;
    }
}

有人能给我一些关于这种现象的可能解释吗?提前谢谢。

P.S。这是我的网络服务器代码:

-module(rudy).
-export([init/2,handler/1,request/1,reply/1, start/2, stop/0]).

start(Port, Concurrency) ->
  register(rudy, spawn(fun() ->
    init(Port, Concurrency) end)).

stop() ->
  exit(whereis(rudy), "time to die").

init(Port, Concurrency) ->
  Opt = [list, {active, false}, {reuseaddr, true}],
  case gen_tcp:listen(Port, Opt) of         % opens a listening socket
    {ok, Listen} ->
      spawn_many(Concurrency, Listen),
      wait_for_threads(Concurrency),
%%       handler(Listen),
      gen_tcp:close(Listen),            % close the socket
      ok;
    {error, Error} -> io:format("Error(~w) from init.~n", [Error])
  end.


wait_for_threads(0) ->
  ok;
wait_for_threads(Count) ->
  receive
    handler_done ->
      wait_for_threads(Count-1)
  end.


handler(Listen) ->
  case gen_tcp:accept(Listen) of            % listen to the socket
    {ok, Client} ->
      request(Client),
      gen_tcp:close(Client),
      handler(Listen);
    {error, Error} -> io:format("Error(~w) from init.~n", [Error])
  end.

request(Client) ->
  Recv = gen_tcp:recv(Client, 0),
  case Recv of
    {ok, Str} ->
      Request = http:parse_request(Str),
      Response = reply(Request),
      gen_tcp:send(Client, Response);
    {error, Error} ->
      io:format("Error(~w) from request.~n", [Error])
  end,
  gen_tcp:close(Client).

reply({{get, URI, _}, _, _}) ->
  timer:sleep(40),
  http:ok(URI).

spawn_many(0, _Listen)-> ok;
spawn_many(N, Listen)->
  spawn(rudy,handler,[Listen]),
  spawn_many(N - 1, Listen).

启动网络服务器的命令是rudy:start(8024, 1000).

0 个答案:

没有答案