title: nginx/bottle python - route request to another port
body: <p>I've looked for the answer around and I believe the answer is pretty simple but I wasn't able to find it. I think for my lack of knowledge of nginx...</p>
  
  <p>I have my nginx instance running on localhost:8080 and my Bottle server listening on localhost:8081. If I open the addresses from the browser, they work fine but when I try to access from the application I'm running on localhost:8080, I cannot retrieve the resources generated by the Bottle server.</p>
  
  <p>What I need to do is redirect all the calls made to the /data/ path to the same domain (localhost) but another port (8081), the one where my bottle server is listening.</p>
  
  <p>Here is the code:
  Nginx:</p>
  
  <pre><code>server {
      listen       8080;
      server_name  localhost;
      root /Users/Diego/Desktop;
  
      location / {
          index  index.html index.htm;
      }
  
      location /data/ {
          proxy_pass http://127.0.0.1:8081;
      }
  }
  </code></pre>
  
  <p>Bottle server:</p>
  
  <pre><code>@route('/')
  def printtest():
      print 'success'
      return 'loaded page'
  
  @route('/&lt;scenename&gt;/data/&lt;filename:path&gt;')
  def testMethod(scenename,filename):
      print scenename, filename
  
  run(host='localhost', port=8081, debug=True)
  </code></pre>
  
  <p>Calling in the browser, localhost:8080, displays me the page served via nginx, but then, if I call a link to retrieve something stored in /data/directory/filename.json, the Bottle does not seem to receive the request. The error log states:</p>
  
  <pre><code>2013/04/16 18:50:52 [error] 3544#10612: *69 CreateFile() "C:/Users/Diego/Desktop/project1/data/directory/directory-varietal.json" failed (3: The system cannot find the path specified), client: 127.0.0.1, server: localhost, request: "GET /project1/data/directory/directory-varietal.json HTTP/1.1", host: "localhost:8081", referrer: "http://localhost:8080/project1/"
  </code></pre>
  
  <p>can anybody tell me how to deal with this kind of redirect/routing?</p>
  
  <p>Plus, is there a way to print a feed in a log from nginx? like a command print_entry or similar?</p>
  
  <p>Thanks!</p>
  
  <p>EDIT: I've tried this but no result... <a href="https://serverfault.com/questions/208656/routing-to-various-node-js-servers-on-same-machine">https://serverfault.com/questions/208656/routing-to-various-node-js-servers-on-same-machine</a></p>
  
  <p>EDIT: Ok some improvement, I found that probably is a matter of the query location. With this block and requesting a .json file it actually queries the Bottle server.</p>
  
  <pre><code>location ~* \.(json)$ {
      proxy_pass http://localhost:8081;
  }
  </code></pre>
  
  <p>EDIT: Yeee! I found a solution... it turned out that it was a problem with the path defined in the location. Note to self: read CAREFULLY the manual: <a href="http://wiki.nginx.org/HttpCoreModule#location" rel="nofollow noreferrer">http://wiki.nginx.org/HttpCoreModule#location</a></p>
  
  <p>new code for the server:</p>
  
  <pre><code>server {
      listen       8080;
      server_name  localhost;
      root /Users/Diego/Desktop;
  
      location / {
          index  index.html index.htm;
      }
  
      location ~* /data/ {
          proxy_pass http://localhost:8081;
      }
  }
  </code></pre>
  
  <p>Anyway if anybody has a better solution or any suggestion is more then welcome to contribute.</p>