【问题标题】:Serving static homepage with nginx and rest through uwsgi使用 nginx 提供静态主页并通过 uwsgi 休息
【发布时间】:2012-01-27 08:28:43
【问题描述】:

我有一个 nginx + uwsgi 网站(将 Flask 用于动态 python 页面)。 我想直接通过 nginx 提供静态主页,并将其他所有内容路由到 uwsgi。

以下 nginx 配置指令可以很好地通过 nginx 为主页提供服务,并将对 mysite.com/login 的调用重定向到 uwsgi:

location / {                                                                                                                                                                                                
    root  /var/www/mysite.com/static;                                                                                                                                                                
    index  index.html index.htm;                                                                                                                                                                            
}                                                                                                                                                                                                           

location /login {                                                                                                                                                                                           
    include uwsgi_params;                                                                                                                                                                                   
    uwsgi_pass 127.0.0.1:3031;                                                                                                                                                                              
} 

但我找不到一种方法来概括第二个指令以捕获对 mysite.com/something 的所有调用并将它们定向到 uwsgi。

我尝试了以下没有用的方法(除了对 mysite.com 的调用之外,得到 404):

location / {                                                                                                                                                                                                
    root  /var/www/mysite.com/static;                                                                                                                                                                
    index  index.html index.htm;                                                                                                                                                                            
}                                                                                                                                                                                                           

location /* {                                                                                                                                                                                           
    include uwsgi_params;                                                                                                                                                                                   
    uwsgi_pass 127.0.0.1:3031;                                                                                                                                                                              
}

有什么建议吗?

【问题讨论】:

    标签: nginx flask uwsgi


    【解决方案1】:

    试试这样的

    server {
    ...
     root  /var/www/mysite.com/static;                                                                                                                                                                
     index  index.html index.htm;   
     try_files $uri @uwsgi; 
     location @uwsgi{
        include uwsgi_params;                                                                                                                                                                                   
        uwsgi_pass 127.0.0.1:3031; 
     }
    ...
    }
    

    http://wiki.nginx.org/HttpCoreModule#try_files

    【讨论】:

    • 谢谢,在将 try_files 指令更改为 try_files $uri $uri/ @uwsgi; 后,它就像一个魅力
    • 注意! '$uri/' 很重要,也解决了我的问题(Dreamhost VPS 上的 nginx 0.8.4)
    • 是否可以仅在特定位置尝试文件?这可能更有效
    猜你喜欢
    • 2013-02-19
    • 1970-01-01
    • 2011-01-27
    • 2015-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-10
    • 1970-01-01
    相关资源
    最近更新 更多