Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

nginx/bottle python - route request to another port

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

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.

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.

Here is the code: Nginx:

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;
    }
}

Bottle server:

@route('/')
def printtest():
    print 'success'
    return 'loaded page'

@route('/<scenename>/data/<filename:path>')
def testMethod(scenename,filename):
    print scenename, filename

run(host='localhost', port=8081, debug=True)

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:

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/"

can anybody tell me how to deal with this kind of redirect/routing?

Plus, is there a way to print a feed in a log from nginx? like a command print_entry or similar?

Thanks!

EDIT: I've tried this but no result... https://serverfault.com/questions/208656/routing-to-various-node-js-servers-on-same-machine

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.

location ~* \.(json)$ {
    proxy_pass http://localhost:8081;
}

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: http://wiki.nginx.org/HttpCoreModule#location

new code for the server:

server {
    listen       8080;
    server_name  localhost;
    root /Users/Diego/Desktop;

    location / {
        index  index.html index.htm;
    }

    location ~* /data/ {
        proxy_pass http://localhost:8081;
    }
}

Anyway if anybody has a better solution or any suggestion is more then welcome to contribute.

like image 287
Dieghito Avatar asked Oct 22 '22 12:10

Dieghito


1 Answers

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: http://wiki.nginx.org/HttpCoreModule#location

new code for the server:

server {
  listen       8080;
  server_name  localhost;
  root /Users/Diego/Desktop;

  location / {
    index  index.html index.htm;
  }

  location ~* /data/ {
    proxy_pass http://localhost:8081;
  }
}

Anyway if anybody has a better solution or any suggestion is more then welcome to contribute.

like image 127
Dieghito Avatar answered Oct 27 '22 10:10

Dieghito