2

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

Farrukh Arshad
  • 131
  • 1
  • 6
  • Farrukh, it would be appreciate if you could answer your own question and mark is answered, rather than just editing your question to include the answer. Unanswered questions get bumped to the home page occasionally, to try to get them answered. – Tim Aug 05 '17 at 23:10

4 Answers4

1

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..

Farrukh Arshad
  • 131
  • 1
  • 6
  • in my case, I have to append subdirectory to the Host header, as `proxy_set_header Host $host/db;`, and the rewrite is unnecessary. And the appended slash `/` is also in need. – Kinka Jan 27 '19 at 02:51
0

I ran into the same issue, you can combine the two rules by using multiple capturing groups:

rewrite /couch(/)?(.*) /$2 break;
jgibson
  • 101
  • 2
0

Here's a configuration that works well for query, view and replication:

location /couchdb/(.*)$ {
    rewrite /couchdb/(.*) /$1 break;
    proxy_pass http://127.0.0.1:5984;
    proxy_pass_header Accept;
    proxy_pass_header Server;
    keepalive_requests 1000;
    add_header 'Access-Control-Allow-Origin' '*';
    proxy_redirect  off;
    proxy_buffering off;
    proxy_set_header Host $host;
    proxy_set_header Authorization ""; # or according to server.ini
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header    X-Real-IP       $remote_addr;
}
AD7six
  • 2,810
  • 2
  • 20
  • 23
Pian0_M4n
  • 178
  • 1
  • 6
0

The answer should be, changed that line proxy_pass http://127.0.0.1:5984 to proxy_pass http://127.0.0.1:5984/. Do you see that, a slash "/" appended to the proxy_pass url. With a slash, nginx will just append the string after "/couchdb/", and couchdb won't be included. That's a subtle trick.

Kinka
  • 101
  • 1