Running Swi Prolog Http Server Behind Nginx

++++

:icons: font

IMPORTANT: This post will remind me how to run the SWI-Prolog http server behind an nginx reverse proxy.

NOTE: This assumes experience with nginx and already have a server up and running.

SWI-Prolog has a built-in webserver in library(http/http_server). Of course exposing this kind of thing directly to the internet is never a good idea and most people put nginx in front of it. SSL can be offloaded to nginx as well.

.Example taken directly from the SWI-Prolog documentation. [source,prolog]

:- use_module(library(http/http_server)).

:- initialization http_server([port(8080)]).

:- http_handler(root(.), http_redirect(moved, location_by_id(home_page)), []). :- http_handler(root(home), home_page, []).

home_page(_Request) :- reply_html_page( title(‘Demo server’), [ h1(‘Hello world!’) ]).

You need to add this piece of code to your nginx.conf. In the server configuration, usually in /etc/nginx/sites-enabled/default:


location /prologcode/ { proxy_pass http://127.0.0.1:8080/; }

Make sure you include the / at the end, because it won’t pass in the path to the prolog http server.