1d4afb5ceSopenharmony_ci## Unix Domain Sockets Reverse Proxy
2d4afb5ceSopenharmony_ci
3d4afb5ceSopenharmony_ci### Introduction
4d4afb5ceSopenharmony_ci
5d4afb5ceSopenharmony_cilws is able to use a mount to place reverse proxies into the URL space.
6d4afb5ceSopenharmony_ci
7d4afb5ceSopenharmony_ciThese are particularly useful when using Unix Domain Sockets, basically
8d4afb5ceSopenharmony_cifiles in the server filesystem, to communicate between lws and a separate
9d4afb5ceSopenharmony_ciserver process and integrate the result into a coherent URL namespace on
10d4afb5ceSopenharmony_cithe lws side.  It's also possible to proxy using tcp sockets.
11d4afb5ceSopenharmony_ci
12d4afb5ceSopenharmony_ci![overview](../doc-assets/http-proxy-overview.svg)
13d4afb5ceSopenharmony_ci
14d4afb5ceSopenharmony_ciThis has the advantage that the actual web server that forwards the
15d4afb5ceSopenharmony_cidata from the unix socket owner is in a different process than the server
16d4afb5ceSopenharmony_cithat serves on the unix socket.  If it has problems, they do not affect
17d4afb5ceSopenharmony_cithe actual public-facing web server.  The unix domain socket server may
18d4afb5ceSopenharmony_cibe in a completely different language than the web server.
19d4afb5ceSopenharmony_ci
20d4afb5ceSopenharmony_ciCompared to CGI, there are no forks to make a connection to the unix
21d4afb5ceSopenharmony_cidomain socket server.
22d4afb5ceSopenharmony_ci
23d4afb5ceSopenharmony_ci### Mount origin format
24d4afb5ceSopenharmony_ci
25d4afb5ceSopenharmony_ciUnix Domain Sockets are effectively "files" in the server filesystem, and
26d4afb5ceSopenharmony_ciare defined by their filepath.  The "server" side that is to be proxied opens
27d4afb5ceSopenharmony_cithe socket and listens on it, which creates a file in the server filesystem.
28d4afb5ceSopenharmony_ciThe socket understands either http or https protocol.
29d4afb5ceSopenharmony_ci
30d4afb5ceSopenharmony_ciLws can be told to act as a proxy for that at a mountpoint in the lws vhost
31d4afb5ceSopenharmony_ciurl space.
32d4afb5ceSopenharmony_ci
33d4afb5ceSopenharmony_ciIf your mount is expressed in C code, then the mount type is LWSMPRO_HTTP or
34d4afb5ceSopenharmony_ciLWSMPRO_HTTPS depending on the protocol the unix socket understands, and the
35d4afb5ceSopenharmony_ciorigin address has the form `+/path/to/unix/socket:/path/inside/mount`.
36d4afb5ceSopenharmony_ci
37d4afb5ceSopenharmony_ciThe + at the start indicates it is a local unix socket we are proxying, and
38d4afb5ceSopenharmony_cithe ':' acts as a delimiter for the socket path, since unlike other addresses
39d4afb5ceSopenharmony_cithe unix socket path can contain '/' itself.
40d4afb5ceSopenharmony_ci
41d4afb5ceSopenharmony_ci### Connectivity rules and translations
42d4afb5ceSopenharmony_ci
43d4afb5ceSopenharmony_ciOnward proxy connections from lws to the Unix Domain Socket happen using
44d4afb5ceSopenharmony_cihttp/1.1.  That implies `transfer-encoding: chunking` in the case that the
45d4afb5ceSopenharmony_cilength of the output is not known beforehand.
46d4afb5ceSopenharmony_ci
47d4afb5ceSopenharmony_ciLws takes care of stripping any chunking (which is illegal in h2) and
48d4afb5ceSopenharmony_citranslating between h1 and h2 header formats if the return connection is
49d4afb5ceSopenharmony_ciactually in http/2.
50d4afb5ceSopenharmony_ci
51d4afb5ceSopenharmony_ciThe h1 onward proxy connection translates the following headers from the return
52d4afb5ceSopenharmony_ciconnection, which may be h1 or h2:
53d4afb5ceSopenharmony_ci
54d4afb5ceSopenharmony_ciHeader|Function
55d4afb5ceSopenharmony_ci---|---
56d4afb5ceSopenharmony_cihost|Which vhost
57d4afb5ceSopenharmony_cietag|Information on any etag the client has cached for this URI
58d4afb5ceSopenharmony_ciif-modified-since|Information on the freshness of any etag the client has cached for this URI
59d4afb5ceSopenharmony_ciaccept-language|Which languages the return path client prefers
60d4afb5ceSopenharmony_ciaccept-encoding|Which compression encodings the client can accept
61d4afb5ceSopenharmony_cicache-control|Information from the return path client about cache acceptability
62d4afb5ceSopenharmony_cix-forwarded-for|The IP address of the return path client
63d4afb5ceSopenharmony_ci
64d4afb5ceSopenharmony_ciThis implies that the proxied connection can
65d4afb5ceSopenharmony_ci
66d4afb5ceSopenharmony_ci - return 301 etc to say the return path client's etag is still valid
67d4afb5ceSopenharmony_ci
68d4afb5ceSopenharmony_ci - choose to compress using an acceptable content-encoding
69d4afb5ceSopenharmony_ci
70d4afb5ceSopenharmony_ciThe following headers are translated from the headers replied via the onward
71d4afb5ceSopenharmony_ciconnection (always h1) back to the return path (which may be h1 or h2)
72d4afb5ceSopenharmony_ci
73d4afb5ceSopenharmony_ciHeader|Function
74d4afb5ceSopenharmony_ci---|---
75d4afb5ceSopenharmony_cicontent-length|If present, an assertion of how much payload is expected
76d4afb5ceSopenharmony_cicontent-type|The mimetype of the payload
77d4afb5ceSopenharmony_cietag|The canonical etag for the content at this URI
78d4afb5ceSopenharmony_ciaccept-language|This is returned to the return path client because there is no easy way for the return path client to know what it sent originally.  It allows clientside selection of i18n.
79d4afb5ceSopenharmony_cicontent-encoding|Any compression format on the payload (selected from what the client sent in accept-encoding, if anything)
80d4afb5ceSopenharmony_cicache-control|The onward server's response about cacheability of its payload
81d4afb5ceSopenharmony_ci
82d4afb5ceSopenharmony_ci### h1 -> h2 conversion
83d4afb5ceSopenharmony_ci
84d4afb5ceSopenharmony_ciChunked encoding that may have been used on the outgoing proxy client connection
85d4afb5ceSopenharmony_ciis removed for h2 return connections (chunked encoding is illegal for h2).
86d4afb5ceSopenharmony_ci
87d4afb5ceSopenharmony_ciHeaders are converted to all lower-case and hpack format for h2 return connections.
88d4afb5ceSopenharmony_ci
89d4afb5ceSopenharmony_ciHeader and payload proxying is staged according to when the return connection
90d4afb5ceSopenharmony_ci(which may be an h2 child stream) is writable.
91d4afb5ceSopenharmony_ci
92d4afb5ceSopenharmony_ci### Behaviour if unix domain socket server unavailable
93d4afb5ceSopenharmony_ci
94d4afb5ceSopenharmony_ciIf the server that listens on the unix domain socket is down or being restarted,
95d4afb5ceSopenharmony_cilws understands that it couldn't connect to it and returns a clean 503 response
96d4afb5ceSopenharmony_ci`HTTP_STATUS_SERVICE_UNAVAILABLE` along with a brief human-readable explanation.
97d4afb5ceSopenharmony_ci
98d4afb5ceSopenharmony_ciThe generated status page produced will try to bring in a stylesheet
99d4afb5ceSopenharmony_ci`/error.css`.  This allows you to produce a styled error pages with logos,
100d4afb5ceSopenharmony_cigraphics etc.  See [this](https://libwebsockets.org/git/badrepo) for an example of what you can do with it.
101d4afb5ceSopenharmony_ci
102