iocaine/docs/content/deploying/nginx.md
Gergely Nagy f5d4985f39
Move documentation to a dedicated site
Signed-off-by: Gergely Nagy <me@gergo.csillger.hu>
2025-01-25 01:31:38 +01:00

1.5 KiB

title description
Using nginx with iocaine Setting up nginx to front for iocaine

Getting started

In here, I assume that iocane has already been configured and deployed. Furthermore, lets assume that we have a site running at [::1]:8080, and we want to serve that with nginx. Normally, that would look something like this:

server {
  server_name blog.example.com;

  location / {
    proxy_set_header Host $host;
    proxy_pass http://[::1]:8080;
  }
}

Routing AI agents elsewhere

To serve something different for AI user agents, the idea is to create a mapping between user-agent and badness, such that AI agents will evaluate to a truthy value, while unmatched against will default to a false-y one. We can do this with a map outside of the server block:

map $http_user_agent $badagent {
  default   0;
  ~*gptbot  1;
  ~*chatgpt 1;
  ~*ccbot   1;
  ~*claude  1;
}

Within the server block, we'll rewrite the URL if find a match on $badagent, and the proxy that location through to iocaine. The reason we need the rewrite is that nginx does not support proxy_pass within an if block. In the end, our server block will look like this:

server {
  server_name blog.example.com;
  if ($badagent) {
    rewrite ^ /ai;
  }
  location /ai {
    proxy_set_header Host $host;
    proxy_pass 127.0.0.1:42069;
  }
  location / {
    proxy_set_header Host $host;
    proxy_pass http://[::1]:8080;
  }
}