Nginx and CouchDB reverse proxy not working

couchdbnginxreverse-proxy

I am trying to proxy [http://localhost:5984] to [http://localhost/couchdb]. I am running nginx for proxy. I have followed the same method mentioned at http://wiki.apache.org/couchdb/Nginx_As_a_Reverse_Proxy,

    location /couchdb {
        rewrite /couchdb/(.*) /$1 break;
        proxy_pass http://127.0.0.1:5984;
        proxy_redirect          off;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header    X-Real-IP       $remote_addr;
    } 

but when I run curl localhost/couchdb I get following error

{"error":"not_found","reason":"no_db_file"}

However when I run curl localhost:5984 I got a valid response from couchdb.

{"couchdb":"Welcome","uuid":"337bb4394efe84536a68a63eee55333f","version":"1.5.0","vendor":    {"name":"The Apache Software Foundation","version":"1.5.0"}}

But when I run curl localhost:5984/couchdb I get the same error (and log) which I am receiving via reverse proxy.

The couchdb log file says following

[Fri, 24 Jan 2014 20:41:29 GMT] [debug] [<0.120.0>] 'GET' /couchdb {1,0} from "127.0.0.1"
Headers: [{'Accept',"*/*"},
      {'Connection',"close"},
      {'Host',"localhost"},
      {'User-Agent',"curl/7.32.0"},
      {'X-Forwarded-For',"127.0.0.1"},
      {"X-Real-Ip","127.0.0.1"}]
[Fri, 24 Jan 2014 20:41:29 GMT] [debug] [<0.120.0>] OAuth Params: []
[Fri, 24 Jan 2014 20:41:29 GMT] [error] [<0.1114.0>] Could not open file /var/lib/couchdb/couchdb.couch: no such file or directory
[Fri, 24 Jan 2014 20:41:29 GMT] [debug] [<0.120.0>] Minor error in HTTP request: {not_found,no_db_file}
[Fri, 24 Jan 2014 20:41:29 GMT] [debug] [<0.120.0>] Stacktrace: [{couch_httpd_db,do_db_req,2,
                                 [{file,"couch_httpd_db.erl"},{line,239}]},
                             {couch_httpd,handle_request_int,5,
                                 [{file,"couch_httpd.erl"},{line,332}]},
                             {mochiweb_http,headers,5,
                                 [{file,"mochiweb_http.erl"},{line,94}]},
                             {proc_lib,init_p_do_apply,3,
                                 [{file,"proc_lib.erl"},{line,239}]}]
[Fri, 24 Jan 2014 20:41:29 GMT] [info] [<0.120.0>] 127.0.0.1 - - GET /couchdb 404
[Fri, 24 Jan 2014 20:41:29 GMT] [debug] [<0.120.0>] httpd 404 error response:
{"error":"not_found","reason":"no_db_file"}

I believe my nginx configuration is correct thats why request is reaching to couchdb. If the missing couchdb.couch file the log says is problem then why this database is not causing trouble when we access it directly on port 5984. It seems the couchdb mochiweb is confusing something.

I am seeing the same behavior on two different distribution

Ubuntu 10.04: CouchDB V 1.10.0
ArchLinux 3.10: CouchDB V 1.5.0

Best Answer

I have solved this by adding

rewrite /couchdb / break;

to access it over localhost/couchdb. The rule which I mentioned

rewrite /couchdb/(.*)  /$1 break;

will work for localhost/couchdb/db1 etc..