<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link rel="icon" href="https://avatars0.githubusercontent.com/u/58360786?s=200&v=4" title="Kayrx">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.10.2/dist/katex.min.css" integrity="sha384-yFRtMMDnQtDRO8rLpMIKrtPCD5jdktao2TV19YiZYWMDkUR5GQZR/NOVTdquEx1j" crossorigin="anonymous">
<link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/katex-copytex@latest/dist/katex-copytex.min.css">
<script src="https://cdn.jsdelivr.net/npm/katex-copytex@latest/dist/katex-copytex.min.js"></script>
<link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/gh/Microsoft/vscode/extensions/markdown-language-features/media/markdown.css">
<link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/gh/Microsoft/vscode/extensions/markdown-language-features/media/highlight.css">
<link rel="stylesheet" type="text/css" href="https://kayrx.xyz/home.css" />
<title>Kayrx / server</title>
</head>
<body class="vscode-light">
<div id="page" class="page-section-doc">
<div id="mnav">
<div id="lnav">
<div id="left">
<a id="front" href="/"><img src="/kayrx.png"/></a>
<span id="dlnav">
<a class="tail" href="/kayrx/" >Kayrx</a>
<a class="tail" href="/yarx" >Yarx</a>
<a class="tail" href="/blog" >Blog</a>
</span>
</div>
<label ><a href="#" id="menu">M</a></label>
</div>
<div id="rnav">
<li > <a href="https://spectrum.chat/kayrx" target="_black" style="color:green; font-weight: bold;" >群聊</a></li>
<li ><a href="/community" >社区</a></li>
<li > <a href="/funding">赞助</a></li>
</div>
</div>
<div id="content">
<div class="container"><div class="doc-page doc-section-basic-server">
<div class="doc-nav">
<details id="pc" open>
<summary id="dir"><strong>目录指南</strong></summary>
<details open>
<summary id="title"><strong>介绍</strong></summary>
<a class="doc-section-link-introduction-index" href="/kayrx/introduction/index.html">欢迎</a>
<a class="doc-section-link-introduction-whatis" href="/kayrx/introduction/whatis.html">何为Actix</a>
<a class="doc-section-link-introduction-install" href="/kayrx/introduction/install.html">安装</a>
</details>
<details open>
<summary id="title"><strong>基本</strong></summary>
<a class="doc-section-link-basic-start" href="/kayrx/basic/start.html">入门</a>
<a class="doc-section-link-basic-app" href="/kayrx/basic/app.html">应用</a>
<a class="doc-section-link-basic-server" href="/kayrx/basic/server.html">服务器</a>
<a class="doc-section-link-basic-handler" href="/kayrx/basic/handler.html">处理器</a>
<a class="doc-section-link-basic-extractor" href="/kayrx/basic/extractor.html">提取器</a>
</details>
<details open>
<summary id="title"><strong>高级</strong></summary>
<a class="doc-section-link-advance-error" href="/kayrx/advance/error.html">错误</a>
<a class="doc-section-link-advance-url-dispatch" href="/kayrx/advance/url-dispatch.html">路由分发</a>
<a class="doc-section-link-advance-request" href="/kayrx/advance/request.html">请求</a>
<a class="doc-section-link-advance-response" href="/kayrx/advance/response.html">响应</a>
<a class="doc-section-link-advance-test" href="/kayrx/advance/test.html">测试</a>
<a class="doc-section-link-advance-middleware" href="/kayrx/advance/middleware.html">中间件</a>
<a class="doc-section-link-advance-static-file" href="/kayrx/advance/static-file.html">静态文件</a>
</details>
<details open>
<summary id="title"><strong>协议</strong></summary>
<a class="doc-section-link-protocol-websocket" href="/kayrx/protocol/websocket.html">Websocket</a>
<a class="doc-section-link-protocol-http2" href="/kayrx/protocol/http2.html">HTTP2</a>
</details>
<details open>
<summary id="title"><strong>模式</strong></summary>
<a class="doc-section-link-pattern-autoreload" href="/kayrx/pattern/autoreload.html">自动重载</a>
<a class="doc-section-link-pattern-database" href="/kayrx/pattern/database.html">数据库</a>
</details>
</details>
<details id="mobile">
<summary id="dir"><strong>目录指南</strong></summary>
<details open>
<summary id="title"><strong>介绍</strong></summary>
<a class="doc-section-link-introduction-index" href="/kayrx/introduction/index.html">欢迎</a>
<a class="doc-section-link-introduction-whatis" href="/kayrx/introduction/whatis.html">何为Actix</a>
<a class="doc-section-link-introduction-install" href="/kayrx/introduction/install.html">安装</a>
</details>
<details open>
<summary id="title"><strong>基本</strong></summary>
<a class="doc-section-link-basic-start" href="/kayrx/basic/start.html">入门</a>
<a class="doc-section-link-basic-app" href="/kayrx/basic/app.html">应用</a>
<a class="doc-section-link-basic-server" href="/kayrx/basic/server.html">服务器</a>
<a class="doc-section-link-basic-handler" href="/kayrx/basic/handler.html">处理器</a>
<a class="doc-section-link-basic-extractor" href="/kayrx/basic/extractor.html">提取器</a>
</details>
<details open>
<summary id="title"><strong>高级</strong></summary>
<a class="doc-section-link-advance-error" href="/kayrx/advance/error.html">错误</a>
<a class="doc-section-link-advance-url-dispatch" href="/kayrx/advance/url-dispatch.html">路由分发</a>
<a class="doc-section-link-advance-request" href="/kayrx/advance/request.html">请求</a>
<a class="doc-section-link-advance-response" href="/kayrx/advance/response.html">响应</a>
<a class="doc-section-link-advance-test" href="/kayrx/advance/test.html">测试</a>
<a class="doc-section-link-advance-middleware" href="/kayrx/advance/middleware.html">中间件</a>
<a class="doc-section-link-advance-static-file" href="/kayrx/advance/static-file.html">静态文件</a>
</details>
<details open>
<summary id="title"><strong>协议</strong></summary>
<a class="doc-section-link-protocol-websocket" href="/kayrx/protocol/websocket.html">Websocket</a>
<a class="doc-section-link-protocol-http2" href="/kayrx/protocol/http2.html">HTTP2</a>
</details>
<details open>
<summary id="title"><strong>模式</strong></summary>
<a class="doc-section-link-pattern-autoreload" href="/kayrx/pattern/autoreload.html">自动重载</a>
<a class="doc-section-link-pattern-database" href="/kayrx/pattern/database.html">数据库</a>
</details>
</details>
</div>
<div class="doc-content"> <h1 id="the-http-server">The HTTP Server</h1>
<p>The <a href="https://docs.rs/actix-web/2/actix_web/struct.HttpServer.html"><strong>HttpServer</strong></a> type is responsible for serving http requests.</p>
<p><code>HttpServer</code> accepts an application factory as a parameter, and the application factory
must have <code>Send</code> + <code>Sync</code> boundaries. More about that in the <em>multi-threading</em> section.</p>
<p>To bind to a specific socket address, <a href="https://docs.rs/actix-web/2/actix_web/struct.HttpServer.html#method.bind"><code>bind()</code></a> must be used, and it may be
called multiple times. To bind ssl socket, <a href="https://docs.rs/actix-web/2/actix_web/struct.HttpServer.html#method.bind_openssl"><code>bind_openssl()</code></a> or
<a href="https://docs.rs/actix-web/2/actix_web/struct.HttpServer.html#method.bind_rustls"><code>bind_rustls()</code></a> should be used. To run the http server, use <code>HttpServer::run()</code>
method.</p>
<p>{{< include-example example="server" section="main" >}}</p>
<p><code>run()</code> method returns instance of <a href="https://docs.rs/actix-web/2/actix_web/dev/struct.Server.html"><code>Server</code></a> type. Methods of server type
could be used for managing http server</p>
<ul>
<li><code>pause()</code> - Pause accepting incoming connections</li>
<li><code>resume()</code> - Resume accepting incoming connections</li>
<li><code>stop()</code> - Stop incoming connection processing, stop all workers and exit</li>
</ul>
<p>Following example shows how to start http server in separate thread.</p>
<p>{{< include-example example="server" file="<a href="http://signals.rs">signals.rs</a>" section="signals" >}}</p>
<h2 id="multi-threading">Multi-threading</h2>
<p><code>HttpServer</code> automatically starts a number of http workers, by default this number is
equal to number of logical CPUs in the system. This number can be overridden with the
<a href="https://docs.rs/actix-web/2/actix_web/struct.HttpServer.html#method.workers"><code>HttpServer::workers()</code></a> method.</p>
<p>{{< include-example example="server" file="<a href="http://workers.rs">workers.rs</a>" section="workers" >}}</p>
<p>The server creates a separate application instance for each created worker. Application state
is not shared between threads. To share state, <code>Arc</code> could be used.</p>
<blockquote>
<p>Application state does not need to be <code>Send</code> or <code>Sync</code>, but application
factory must be <code>Send</code> + <code>Sync</code>.</p>
</blockquote>
<h2 id="ssl">SSL</h2>
<p>There are two features for the ssl server: <code>rustls</code> and <code>openssl</code>. The <code>rustls</code> feature is for
<code>rustls</code> integration and <code>openssl</code> is for <code>openssl</code>.</p>
<pre><code class="language-toml"><div><span class="hljs-section">[dependencies]</span>
<span class="hljs-attr">actix-web</span> = { version = <span class="hljs-string">"{{< actix-version "</span>actix-web<span class="hljs-string">" >}}"</span>, features = [<span class="hljs-string">"openssl"</span>] }
<span class="hljs-attr">openssl</span> = { version=<span class="hljs-string">"0.10"</span> }
</div></code></pre>
<p>{{< include-example example="server" file="<a href="http://ssl.rs">ssl.rs</a>" section="ssl" >}}</p>
<blockquote>
<p><strong>Note</strong>: the <em>HTTP/2.0</em> protocol requires <a href="https://tools.ietf.org/html/rfc7301">tls alpn</a>.
At the moment, only <code>openssl</code> has <code>alpn</code> support.
For a full example, check out <a href="https://github.com/actix/examples/tree/master/tls">examples/tls</a>.</p>
</blockquote>
<p>To create the key.pem and cert.pem use the command. <strong>Fill in your own subject</strong></p>
<pre><code class="language-bash"><div>$ openssl req -x509 -newkey rsa:4096 -keyout key.pem -out cert.pem \
-days 365 -sha256 -subj <span class="hljs-string">"/C=CN/ST=Fujian/L=Xiamen/O=TVlinux/OU=Org/CN=muro.lxd"</span>
</div></code></pre>
<p>To remove the password, then copy nopass.pem to key.pem</p>
<pre><code class="language-bash"><div>$ openssl rsa -<span class="hljs-keyword">in</span> key.pem -out nopass.pem
</div></code></pre>
<h2 id="keep-alive">Keep-Alive</h2>
<p>Actix can wait for requests on a keep-alive connection.</p>
<blockquote>
<p><em>keep alive</em> connection behavior is defined by server settings.</p>
</blockquote>
<ul>
<li><code>75</code>, <code>Some(75)</code>, <code>KeepAlive::Timeout(75)</code> - enable 75 second <em>keep alive</em> timer.</li>
<li><code>None</code> or <code>KeepAlive::Disabled</code> - disable <em>keep alive</em>.</li>
<li><code>KeepAlive::Tcp(75)</code> - use <code>SO_KEEPALIVE</code> socket option.</li>
</ul>
<p>{{< include-example example="server" file="keep_alive.rs" section="keep-alive" >}}</p>
<p>If the first option above is selected, then <em>keep alive</em> state is calculated based on the
response's <em>connection-type</em>. By default <code>HttpResponse::connection_type</code> is not
defined. In that case <em>keep alive</em> is defined by the request's http version.</p>
<blockquote>
<p><em>keep alive</em> is <strong>off</strong> for <em>HTTP/1.0</em> and is <strong>on</strong> for <em>HTTP/1.1</em> and <em>HTTP/2.0</em>.</p>
</blockquote>
<p><em>Connection type</em> can be changed with <code>HttpResponseBuilder::connection_type()</code> method.</p>
<p>{{< include-example example="server" file="keep_alive_tp.rs" section="example" >}}</p>
<h2 id="graceful-shutdown">Graceful shutdown</h2>
<p><code>HttpServer</code> supports graceful shutdown. After receiving a stop signal, workers
have a specific amount of time to finish serving requests. Any workers still alive after the
timeout are force-dropped. By default the shutdown timeout is set to 30 seconds. You
can change this parameter with the <a href="https://docs.rs/actix-web/2/actix_web/struct.HttpServer.html#method.shutdown_timeout"><code>HttpServer::shutdown_timeout()</code></a>
method.</p>
<p>You can send a stop message to the server with the server address and specify if you want
graceful shutdown or not. The <a href="https://docs.rs/actix-web/2/actix_web/struct.HttpServer.html#method.start"><code>start()</code></a> method returns address of the server.</p>
<p><code>HttpServer</code> handles several OS signals. <em>CTRL-C</em> is available on all OSs, other signals
are available on unix systems.</p>
<ul>
<li><em>SIGINT</em> - Force shutdown workers</li>
<li><em>SIGTERM</em> - Graceful shutdown workers</li>
<li><em>SIGQUIT</em> - Force shutdown workers</li>
</ul>
<blockquote>
<p>It is possible to disable signal handling with
<a href="https://docs.rs/actix-web/2/actix_web/struct.HttpServer.html#method.disable_signals"><code>HttpServer::disable_signals()</code></a> method.</p>
</blockquote>
</div>
</div>
</div>
</div>
<div id="footer">
© 2019 <a href="https://github.com/kayrx">Kayrx</a>
</div>
</div>
<script async>
window.onload = function() {
let menu = document.getElementById('menu');
menu.addEventListener('click', function() {
let nav = document.getElementById('rnav');
if (nav.style.height == 'auto') {
nav.style.height = '0';
}else{
nav.style.height = 'auto';
}
}, false)
}
</script>
</body>
</html>