Skip to content

Instantly share code, notes, and snippets.

@EpicKiwi
Created January 15, 2022 17:40
Show Gist options
  • Save EpicKiwi/10fd5119d0fee8f0cbb470237e89c0ef to your computer and use it in GitHub Desktop.
Save EpicKiwi/10fd5119d0fee8f0cbb470237e89c0ef to your computer and use it in GitHub Desktop.
Nginx configuration for couchapp proxying with pretty urls

Couchapp NGINX config

Small nginx configuration in front of couchdb

Features

  • Url rewriting for pretty website hierarchy
  • Basic authentication on private databases

URL rewriting

  • /{db} > /{db}/_design/index/index.html
  • /{db}/{endpoint} > /{db}/_design/{endpoint}/index.html
  • /{db}/{doc}/{endpoint} > /{db}/_design/{doc}/{endpoint}.html
  • /{db}/{file.ext} > `/{db}/_design/index/{file.ext}·
  • /{db}/{endpoint}/.../{file.ext} > /{db}/_design/{endpoint}/{file.ext}

All requests on /api are passed to couchdb directly

upstream couchdb {
server couchdb:5984;
}
server {
listen 80;
listen [::]:80;
location /api {
client_max_body_size 5G;
rewrite ^/api/?(.*)$ /$1 break;
proxy_pass http://couchdb;
}
location @forbidden {
add_header WWW-Authenticate 'Basic realm="Private app"' always;
return 401;
}
location @notfound {
return 404;
}
location / {
# /{db} > /{db}/_design/index/index.html
rewrite ^/([^/]+)/$ /$1/_design/index/index.html break;
rewrite ^/([^/]+)$ /$1/ redirect;
# /{db}/{endpoint} > /{db}/_design/{endpoint}/index.html
rewrite ^/([^/]+)/([^/.]+)/$ /$1/_design/$2/index.html break;
rewrite ^/([^/]+)/([^/.]+)$ /$1/$2/ redirect;
# /{db}/{doc}/{endpoint} > /{db}/_design/{doc}/{endpoint}.html
rewrite ^/([^/]+)/([^/]+)/([^/.]+)/$ /$1/_design/$2/$3.html break;
rewrite ^/([^/]+)/([^/.]+)/([^/.]+)$ /$1/$2/$3/ redirect;
# /{db}/{file.ext} > /{db}/_design/index/{file.ext}
rewrite ^/([^/]+)/([^/]+\.[^/.]+)$ /$1/_design/index/$2 break;
# /{db}/{endpoint}/.../{file.ext} > /{db}/_design/{endpoint}/{file.ext}
rewrite ^/([^/]+)/([^/]+)/([^/]+\.[^/.]+)$ /$1/_design/$2/$3 break;
rewrite ^/([^/]+)/([^/]+)/([^/]+/)*([^/]+\.[^/.]+)$ /$1/$2/$4 redirect;
rewrite ^.*$ @notfound last;
proxy_intercept_errors on;
error_page 401 @forbidden;
error_page 404 @notfound;
proxy_pass http://couchdb;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment