【发布时间】:2013-04-09 15:39:23
【问题描述】:
我一直在寻找答案,我相信答案很简单,但我找不到。我认为由于我对 nginx 的了解不足......
我的 nginx 实例在 localhost:8080 上运行,而我的 Bottle 服务器在 localhost:8081 上侦听。如果我从浏览器打开地址,它们可以正常工作,但是当我尝试从我在 localhost:8080 上运行的应用程序访问时,我无法检索 Bottle 服务器生成的资源。
我需要做的是将所有对 /data/ 路径的调用重定向到同一个域 (localhost) 但另一个端口 (8081),即我的 Bottle 服务器正在侦听的端口。
代码如下: 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;
}
}
无论如何,如果有人有更好的解决方案或任何建议,欢迎贡献。
【问题讨论】:
-
很高兴听到您解决了这个问题——感谢您回来发布解决方案!
-
@Dieghito,请发布您的更新作为正确答案。可以回答您自己的问题。
-
我对您描述中的端口与您的代码感到困惑。您说 nginx 实例在 localhost:8080 上运行,而我的 Bottle 服务器正在侦听 localhost:8081 ,但 nginx 配置显示
listen 8081;和 bottleport=8080。 -
感谢,已编辑并修复
标签: python redirect nginx port bottle