Skip to content

Instantly share code, notes, and snippets.

@matsumotory
Created July 20, 2017 03:04
Show Gist options
  • Save matsumotory/91678fe41fb65f55bfbc6f574327552e to your computer and use it in GitHub Desktop.
Save matsumotory/91678fe41fb65f55bfbc6f574327552e to your computer and use it in GitHub Desktop.
# $ ulimit -n 60000

worker_processes  1;

events {
    worker_connections  30000;
}

daemon off;
master_process off;
error_log logs/error.log debug;

stream {
  upstream dynamic_server {
    server 127.0.0.1:8080;
  }

  server {
      mruby_stream_server_context_code '
        (20001..30000).each { |port| Nginx::Stream.add_listener({address: port.to_s}) }
      ';

      mruby_stream_code '
        c = Nginx::Stream::Connection.new "dynamic_server"
        c.upstream_server = "127.0.0.1:#{Nginx::Stream::Connection.local_port * 2}"
      ';

      proxy_pass dynamic_server;
  }
}

http {
    server {
        mruby_server_context_handler_code '
          s = Nginx::Server.new
          (20001..30000).each { |port| s.add_listener({address: (port * 2).to_s}) }
        ';

        location /mruby {
          mruby_content_handler_code 'Nginx.rputs "#{Nginx::Connection.new.local_port} sann hello"';
        }
    }
}
@matsumotory
Copy link
Author

[ubuntu@ubuntu-xenial:~]$ curl http://127.0.0.1:20001/mruby
40002 sann hello
[ubuntu@ubuntu-xenial:~]$ curl http://127.0.0.1:20002/mruby
40004 sann hello
[ubuntu@ubuntu-xenial:~]$ curl http://127.0.0.1:29999/mruby
59998 sann hello

@matsumotory
Copy link
Author

$ netstat -lnpt | grep nginx | wc -l
20000

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment