Ocsigen Server is a powerful and modular generic purpose Web Server. It supports static files, redirections, reverse proxy, CORS, user pages with local configuration, page compression, authentication, etc.
Ocsigen Server can be used:
It is written in OCaml and uses the Cohttp library.
The easier way to install Ocsigenserver is to use the opam package manager. Ocsigen Server is also available in Linux distributions.
To serve the static files of a directory without writing any configuration file, just give the directory to ocsigenserver:
ocsigenserver ./public
This starts a server on port 8080 serving the files of ./public. No configuration file and no log directory are needed: the logs go to the terminal. A few options are available:
ocsigenserver --serve ./public --port 8000 --directory-listing
You can also start a reverse proxy that forwards every request to another server, again without any configuration file:
ocsigenserver --reverse-proxy http://localhost:9000 --port 8080
This is the simplest way to get started. For anything more elaborate (several hosts, extensions, fine-grained reverse proxy rules, etc.) use a configuration file or the library API, as described below.
Create your configuration file. You can take this one as an example:
<ocsigen>
<server>
<port>8080</port>
<logdir>local/var/log/mysite</logdir>
<datadir>local/var/data/mysite</datadir>
<charset>utf-8</charset>
<commandpipe>local/var/run/mysite-cmd</commandpipe>
<extension findlib-package="ocsigenserver.ext.staticmod"/>
<host hostfilter="*">
<static dir="local/var/www/mysite/" />
</host>
</server>
</ocsigen>Update the paths to match your configuration and create the missing directories. Then run:
ocsigenserver -c <name of your configuration file>
Have a look at the page Configuration to learn how to personalise the configuration file and adapt it to your needs.
If you want to include a Web Server in your OCaml app, add library ocsigenserver and the extensions you need (here ocsigenserver.ext.staticmod) in your Dune file:
(executable (public_name myapp) (name main) (libraries ocsigenserver ocsigenserver.ext.staticmod))
Call function Ocsigen.Server.start like this to run the server:
let _ =
Ocsigen.Server.start
[ Ocsigen.Server.host [Staticmod.run ~dir:"local/var/www/mysite/" ()]]The server runs on port 8080 by default. Customise this using optional parameters of function Ocsigen.Server.start
Depending on the features you need, you can extend the server by loading some modules.
The extensions provided are:
If you are using a configuration, file, use tag <extension/> to load an exetnsion (see examples in the manual of each extension). Most extension have configuration options, and define new tags for the configuration file.
If you are building a static executable, load the extensions as any library from your Dune file. Have a look at the API documentation of each documentation to see the functions it defines and the configuration options.
See the API reference for all the modules, functions and configuration options provided by Ocsigen Server and its extensions.
Ocsigen Server 8.0 renamed its modules and modernised its base library. See Migrating to Ocsigen Server 8.0 for the details and for the ocsigenserver-compat package that eases the transition.
It is not recommended to run a Web server as root. If you want to use priviledged ports (port numbers less than 1024), like 80 or 443, you can add the capability to ocsigenserver with:
setcap 'cap_net_bind_service=+ep' <path to the server executable>
If something goes wrong, first have a look in the logs to see if there is an error message. The directory of logs is set in the configuration file or through option ?logdir of function Ocsigen.Server.start ($OPAM_SWITCH_PREFIX/lib/ocsigenserver/var/log/ocsigenserver by default).
If you cannot solve your problem, if you find a bug, a mistake in the documentation (even a small one), or if you want to make a suggestion, open an issue on Github.