V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
MayKiller
V2EX  ›  Python

Flask route 变量规则 <path:xxx> 部署到 ngnix 后多斜杠碰到的奇怪问题

  •  
  •   MayKiller · 2016-05-17 20:51:05 +08:00 · 4586 次点击
    这是一个创建于 2898 天前的主题,其中的信息可能已经有所发展或是发生改变。

    刚接触 Flask, 在部署到 nginx 后 route 变量规则这里碰到了奇怪的问题...

    from flask import Flask
    app = Flask(__name__)
    
    @app.route('/geturl/<path:url>')
    def geturl(url):
        return url
    
    if __name__=='__main__':
        app.run()
    

    直接用 python 执行 python geturl.py 访问 http://127.0.0.1:5000/geturl/http://google.com, 返回:

    http://google.com
    

    然后用 uwsgi 部署到 nginx 后访问该地址返回:

    http:/google.com
    

    发现少了一个斜杠, 于是后面的地址用 urlencode 尝试

    http://127.0.0.1:5000/geturl/http%3A%2F%2Fgoogle.com

    仍然返回

    http:/google.com
    

    为什么莫名其妙的少了一个斜杠... 之后尝试, 无论多少个斜杠连在一起都只返回一个?!

    如果是 nginx 的锅, 那该怎么让它支持多个连续斜杠...

    第 1 条附言  ·  2016-05-17 21:33:32 +08:00

    感谢 @Zzzzzzzzz, 和下面小伙伴的回答. 已解决, 果然是 nginx 的锅.

    添加 merge_slashes off; 后就好了..

    虽然这样可以用了, 但是还是打算用 localhost/geturl?url=http://google.com 配合 request.args.get 好了.

    这样在不设置 merge_slashes off; 的情况下也可以正常工作

    5 条回复    2016-05-17 21:29:25 +08:00
    Kilerd
        1
    Kilerd  
       2016-05-17 21:14:09 +08:00
    确定 是 nginx 的 锅?

    试试 urldecode 先?
    Zzzzzzzzz
        2
    Zzzzzzzzz  
       2016-05-17 21:18:45 +08:00   ❤️ 1
    nginx 里设置 merge_slashes off;
    lxy
        3
    lxy  
       2016-05-17 21:18:51 +08:00
    Try escaping "%" as "%25"
    MayKiller
        4
    MayKiller  
    OP
       2016-05-17 21:25:00 +08:00
    @Kilerd
    直接
    python geturl.py
    或者
    uwsgi --protocol http --socket 127.0.0.1:5000 --plugin python --module geturl --callable app
    都可以正常的返回多斜杠的内容...

    只不过部署到 nginx 之后... 莫名其妙的就不能多斜杠了.

    > 试试 urldecode 先?
    怎么试...
    MayKiller
        5
    MayKiller  
    OP
       2016-05-17 21:29:25 +08:00
    @Zzzzzzzzz 感谢, 果然是 nginx 的锅... 之前找半天找不到解决办法
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5786 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 30ms · UTC 01:56 · PVG 09:56 · LAX 18:56 · JFK 21:56
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.