CouchDB多参数虚拟主机与URL重写问题求助
Hey Richard, sorry to hear you're stuck navigating CouchDB's vhost and rewrite rules—those official docs can feel pretty vague when you're trying to tie them to your existing setup with 30 design docs and 1900+ documents. Let's walk through this step by step to get you sorted out.
First, let's start with the basics that often trip people up when working with vhosts and rewrites:
1. Lock in Your Virtual Host Base Setup
Fauxton's vhost UI is indeed not the most intuitive, so let's go straight to the config file (local.ini) or use the HTTP API to set this properly:
- Open your
local.iniand find the[vhosts]section - Add a line mapping your domain to your database's rewrite endpoint, like:
Note: I used a "root" design doc here to hold top-level rewrite rules that route to your 30 type-specific design docs—this keeps things clean instead of hardcoding 30 vhost entries.myapp.yourdomain.com=/your-db-name/_design/root-rewrite/_rewrite
2. Write Rewrite Rules That Map to Your Type-Specific Design Docs
In that root rewrite design doc, add a rewrites array that routes requests based on the type field. For example:
{ "_id": "_design/root-rewrite", "rewrites": [ // Route requests like /article to the article design doc's all view { "from": "/:type", "to": "_design/:type/_view/all", "query": {"include_docs": "true"}, "method": "GET" }, // Route requests like /article/123 to fetch the specific document { "from": "/:type/:docid", "to": "/:docid", "method": "GET" } ] }
Each of your 30 type-specific design docs should have a view named all that filters by type (e.g., function(doc) { if (doc.type === "article") emit(null, doc); }).
3. Fix Common Pitfalls You Might Have Missed
- Double-check rewrite field placement: Make sure the
rewritesarray is a top-level field in your design docs—not nested underviewsor another key. - Verify vhost permissions: Ensure
httpd/vhost_global_handlersinlocal.iniincludes_rewrite(it should by default, but it's worth checking):httpd/vhost_global_handlers = _rewrite,_jsonp,_events,_session - Test with curl instead of Fauxton: Fauxton can hide error details. Run a test request like:
The error response will tell you exactly if it's a missing design doc, bad rewrite rule, or vhost misconfiguration.curl -X GET http://myapp.yourdomain.com/article - Match type names exactly: Your design doc IDs (e.g.,
_design/article) must perfectly match thetypevalue in your documents—case sensitivity matters here!
4. Optimize for Your 30 Design Docs & 1900 Documents
Since you've got a lot of type-specific design docs, here are a few tips to keep things maintainable:
- Consider consolidating similar types: If some types share view logic, group them into a single design doc and use rewrite rules to filter by type.
- Pre-build views: Enable background view updates in
local.inito avoid slow first requests:view_index_background_update = true - Use _list functions: Add
_listfunctions to your design docs to format view results directly in CouchDB, reducing client-side processing.
If you're still hitting errors, share a snippet of your current vhost config, one of your type-specific design docs, and the exact error message you're getting—we can dive deeper from there!
内容的提问来源于stack exchange,提问作者Richard Wood

