1Mojo::Server::Daemon(3)User Contributed Perl DocumentatioMnojo::Server::Daemon(3)
2
3
4
6 Mojo::Server::Daemon - Non-blocking I/O HTTP and WebSocket server
7
9 use Mojo::Server::Daemon;
10
11 my $daemon = Mojo::Server::Daemon->new(listen => ['http://*:8080']);
12 $daemon->unsubscribe('request')->on(request => sub {
13 my ($daemon, $tx) = @_;
14
15 # Request
16 my $method = $tx->req->method;
17 my $path = $tx->req->url->path;
18
19 # Response
20 $tx->res->code(200);
21 $tx->res->headers->content_type('text/plain');
22 $tx->res->body("$method request for $path!");
23
24 # Resume transaction
25 $tx->resume;
26 });
27 $daemon->run;
28
30 Mojo::Server::Daemon is a full featured, highly portable non-blocking
31 I/O HTTP and WebSocket server, with IPv6, TLS, SNI, Comet (long
32 polling), keep-alive and multiple event loop support.
33
34 For better scalability (epoll, kqueue) and to provide non-blocking name
35 resolution, SOCKS5 as well as TLS support, the optional modules EV
36 (4.32+), Net::DNS::Native (0.15+), IO::Socket::Socks (0.64+) and
37 IO::Socket::SSL (2.009+) will be used automatically if possible.
38 Individual features can also be disabled with the "MOJO_NO_NNR",
39 "MOJO_NO_SOCKS" and "MOJO_NO_TLS" environment variables.
40
41 See "DEPLOYMENT" in Mojolicious::Guides::Cookbook for more.
42
44 The Mojo::Server::Daemon process can be controlled at runtime with the
45 following signals.
46
47 INT, TERM
48 Shut down server immediately.
49
51 Mojo::Server::Daemon inherits all events from Mojo::Server.
52
54 Mojo::Server::Daemon inherits all attributes from Mojo::Server and
55 implements the following new ones.
56
57 acceptors
58 my $acceptors = $daemon->acceptors;
59 $daemon = $daemon->acceptors(['6be0c140ef00a389c5d039536b56d139']);
60
61 Active acceptor ids.
62
63 # Check port
64 mu $port = $daemon->ioloop->acceptor($daemon->acceptors->[0])->port;
65
66 backlog
67 my $backlog = $daemon->backlog;
68 $daemon = $daemon->backlog(128);
69
70 Listen backlog size, defaults to "SOMAXCONN".
71
72 inactivity_timeout
73 my $timeout = $daemon->inactivity_timeout;
74 $daemon = $daemon->inactivity_timeout(5);
75
76 Maximum amount of time in seconds a connection with an active request
77 can be inactive before getting closed, defaults to the value of the
78 "MOJO_INACTIVITY_TIMEOUT" environment variable or 30. Setting the value
79 to 0 will allow connections to be inactive indefinitely.
80
81 ioloop
82 my $loop = $daemon->ioloop;
83 $daemon = $daemon->ioloop(Mojo::IOLoop->new);
84
85 Event loop object to use for I/O operations, defaults to the global
86 Mojo::IOLoop singleton.
87
88 keep_alive_timeout
89 my $timeout = $daemon->keep_alive_timeout;
90 $daemon = $daemon->keep_alive_timeout(10);
91
92 Maximum amount of time in seconds a connection without an active
93 request can be inactive before getting closed, defaults to the value of
94 the "MOJO_KEEP_ALIVE_TIMEOUT" environment variable or 5. Setting the
95 value to 0 will allow connections to be inactive indefinitely.
96
97 listen
98 my $listen = $daemon->listen;
99 $daemon = $daemon->listen(['https://127.0.0.1:8080']);
100
101 Array reference with one or more locations to listen on, defaults to
102 the value of the "MOJO_LISTEN" environment variable or "http://*:3000"
103 (shortcut for "http://0.0.0.0:3000").
104
105 # Listen on all IPv4 interfaces
106 $daemon->listen(['http://*:3000']);
107
108 # Listen on all IPv4 and IPv6 interfaces
109 $daemon->listen(['http://[::]:3000']);
110
111 # Listen on IPv6 interface
112 $daemon->listen(['http://[::1]:4000']);
113
114 # Listen on IPv4 and IPv6 interfaces
115 $daemon->listen(['http://127.0.0.1:3000', 'http://[::1]:3000']);
116
117 # Listen on UNIX domain socket "/tmp/myapp.sock" (percent encoded slash)
118 $daemon->listen(['http+unix://%2Ftmp%2Fmyapp.sock']);
119
120 # File descriptor, as used by systemd
121 $daemon->listen(['http://127.0.0.1?fd=3']);
122
123 # Allow multiple servers to use the same port (SO_REUSEPORT)
124 $daemon->listen(['http://*:8080?reuse=1']);
125
126 # Listen on two ports with HTTP and HTTPS at the same time
127 $daemon->listen(['http://*:3000', 'https://*:4000']);
128
129 # Use a custom certificate and key
130 $daemon->listen(['https://*:3000?cert=/x/server.crt&key=/y/server.key']);
131
132 # Domain specific certificates and keys (SNI)
133 $daemon->listen(
134 ['https://*:3000?example.com_cert=/x/my.crt&example.com_key=/y/my.key']);
135
136 # Or even a custom certificate authority
137 $daemon->listen(
138 ['https://*:3000?cert=/x/server.crt&key=/y/server.key&ca=/z/ca.crt']);
139
140 These parameters are currently available:
141
142 ca
143 ca=/etc/tls/ca.crt
144
145 Path to TLS certificate authority file used to verify the peer
146 certificate.
147
148 cert
149 cert=/etc/tls/server.crt
150 mojolicious.org_cert=/etc/tls/mojo.crt
151
152 Path to the TLS cert file, defaults to a built-in test certificate.
153
154 ciphers
155 ciphers=AES128-GCM-SHA256:RC4:HIGH:!MD5:!aNULL:!EDH
156
157 TLS cipher specification string. For more information about the
158 format see
159 <https://www.openssl.org/docs/manmaster/man1/ciphers.html#CIPHER-STRINGS>.
160
161 fd
162 fd=3
163
164 File descriptor with an already prepared listen socket.
165
166 key
167 key=/etc/tls/server.key
168 mojolicious.org_key=/etc/tls/mojo.key
169
170 Path to the TLS key file, defaults to a built-in test key.
171
172 reuse
173 reuse=1
174
175 Allow multiple servers to use the same port with the "SO_REUSEPORT"
176 socket option.
177
178 single_accept
179 single_accept=1
180
181 Only accept one connection at a time.
182
183 verify
184 verify=0x00
185
186 TLS verification mode.
187
188 version
189 version=TLSv1_2
190
191 TLS protocol version.
192
193 max_clients
194 my $max = $daemon->max_clients;
195 $daemon = $daemon->max_clients(100);
196
197 Maximum number of accepted connections this server is allowed to handle
198 concurrently, before stopping to accept new incoming connections,
199 passed along to "max_connections" in Mojo::IOLoop.
200
201 max_requests
202 my $max = $daemon->max_requests;
203 $daemon = $daemon->max_requests(250);
204
205 Maximum number of keep-alive requests per connection, defaults to 100.
206
207 silent
208 my $bool = $daemon->silent;
209 $daemon = $daemon->silent($bool);
210
211 Disable console messages.
212
214 Mojo::Server::Daemon inherits all methods from Mojo::Server and
215 implements the following new ones.
216
217 ports
218 my $ports = $daemon->ports;
219
220 Get all ports this server is currently listening on.
221
222 # All ports
223 say for @{$daemon->ports};
224
225 run
226 $daemon->run;
227
228 Run server and wait for "SIGNALS".
229
230 start
231 $daemon = $daemon->start;
232
233 Start or resume accepting connections through "ioloop".
234
235 # Listen on random port
236 my $port = $daemon->listen(['http://127.0.0.1'])->start->ports->[0];
237
238 # Run multiple web servers concurrently
239 my $daemon1 = Mojo::Server::Daemon->new(listen => ['http://*:3000'])->start;
240 my $daemon2 = Mojo::Server::Daemon->new(listen => ['http://*:4000'])->start;
241 Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
242
243 stop
244 $daemon = $daemon->stop;
245
246 Stop accepting connections through "ioloop".
247
249 You can set the "MOJO_SERVER_DEBUG" environment variable to get some
250 advanced diagnostics information printed to "STDERR".
251
252 MOJO_SERVER_DEBUG=1
253
255 Mojolicious, Mojolicious::Guides, <https://mojolicious.org>.
256
257
258
259perl v5.32.0 2020-07-28 Mojo::Server::Daemon(3)