代码之家  ›  专栏  ›  技术社区  ›  Dieghito

nginx/parter-python-将请求路由到另一个端口

  •  5
  • Dieghito  · 技术社区  · 12 年前

    我四处寻找答案,我相信答案很简单,但我没能找到。我认为由于我对nginx缺乏了解。。。

    我的nginx实例在localhost:8080上运行,Bottle服务器在localhost:8081上侦听。如果我从浏览器打开地址,它们可以正常工作,但当我试图从我在localhost:8080上运行的应用程序访问时,我无法检索Bottle服务器生成的资源。

    我需要做的是将所有对/data/路径的调用重定向到同一个域(localhost),但重定向到另一个端口(8081),即我的瓶子服务器正在侦听的端口。

    这是代码: 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;
        }
    }
    

    瓶子服务器:

    @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)
    

    在浏览器中调用localhost:8080,会显示通过nginx提供的页面,但如果我调用链接来检索存储在/data/directory/filename.json中的内容,Bottle似乎不会收到请求。错误日志状态为:

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

    有人能告诉我如何处理这种重定向/路由吗?

    另外,有没有办法在nginx的日志中打印提要?像命令print_entry或类似的?

    谢谢

    编辑:我试过了,但没有结果。。。 https://serverfault.com/questions/208656/routing-to-various-node-js-servers-on-same-machine

    编辑:好的,有些改进,我发现这可能是查询位置的问题。使用这个块并请求一个.json文件,它实际上会查询Bottle服务器。

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

    编辑:耶!我找到了一个解决方案。。。结果发现,该位置中定义的路径有问题。自我注意:仔细阅读手册: http://wiki.nginx.org/HttpCoreModule#location

    服务器的新代码:

    server {
        listen       8080;
        server_name  localhost;
        root /Users/Diego/Desktop;
    
        location / {
            index  index.html index.htm;
        }
    
        location ~* /data/ {
            proxy_pass http://localhost:8081;
        }
    }
    

    无论如何,如果有人有更好的解决方案或任何建议,那么欢迎贡献。

    1 回复  |  直到 8 年前
        1
  •  3
  •   Dieghito    11 年前

    编辑:耶!我找到了一个解决方案。。。结果发现,该位置中定义的路径有问题。自我注意:仔细阅读手册: http://wiki.nginx.org/HttpCoreModule#location

    服务器的新代码:

    server {
      listen       8080;
      server_name  localhost;
      root /Users/Diego/Desktop;
    
      location / {
        index  index.html index.htm;
      }
    
      location ~* /data/ {
        proxy_pass http://localhost:8081;
      }
    }
    

    无论如何,如果有人有更好的解决方案或任何建议,那么欢迎贡献。