简体   繁体   English

(nginx + uWSGI + Bottle)服务静态文件

[英](nginx + uWSGI + Bottle) Serve static Files

I decided to use Python as a primary language on my starting up website. 我决定在我的启动网站上使用Python作为主要语言。 I'm pretty sure that uWSGI and Bottle framework work perfect together. 我很确定uWSGI和Bottle框架可以完美地协同工作。 I'm a little bit worry that they will serve static files slowly ( I experienced this problem in NodeJS ). 我有点担心它们会缓慢地提供静态文件(我在NodeJS中遇到了这个问题)。 Is it preferably to specify multiple uWSGI apps and point them to different directories that don't contain static files? 是否最好指定多个uWSGI应用并将它们指向不包含静态文件的不同目录? Will Nginx serve static files faster? Nginx会更快地提供静态文件吗?

ROOT/
|--assets/
|----some.css
|----and_image.png
|--robots.txt

sign_in/
|--[application related files here]
sign_up/
|--[application related files here]

Web server's root is ROOT and application's and directories that contain files of application are outside the web server's root. Web服务器的根目录是ROOT ,包含应用程序文件的应用程序和目录位于Web服务器的根目录之外。

I suppose this will be better 我想这会更好

location /sign-in {
    uwsgi_pass      unix:///run/uwsgi/app/sign-in/sign-in.co.socket;
    include         uwsgi_params;
    uwsgi_param     UWSGI_SCHEME $scheme;
    uwsgi_param     SERVER_SOFTWARE    nginx/$nginx_version;
}


location /sign-up {
    uwsgi_pass      unix:///run/uwsgi/app/sign-up/sign-up.co.socket;
    include         uwsgi_params;
    uwsgi_param     UWSGI_SCHEME $scheme;
    uwsgi_param     SERVER_SOFTWARE    nginx/$nginx_version;
}

than this: 比这个:

location / {
    uwsgi_pass      unix:///run/uwsgi/app/whole-website/whole-website.co.socket;
    include         uwsgi_params;
    uwsgi_param     UWSGI_SCHEME $scheme;
    uwsgi_param     SERVER_SOFTWARE    nginx/$nginx_version;
}

Is it really better? 真的更好吗? Or in this case uWSGI won't serve static files? 还是在这种情况下,uWSGI无法提供静态文件?

You want nginx to serve your static files. 您希望nginx提供您的静态文件。 Given they are static, there's no logic required to serve them, unlike your templates/views in bottle. 由于它们是静态的,因此不需要逻辑来服务它们,这与您瓶中的模板/视图不同。 So its better for static file requests to never have to hit python. 因此,对于静态文件请求而言,永远不必打python更好。 And its really easy to set up in nginx! 而且在nginx中设置起来非常容易!

Inside your server block just add: 在您的服务器块内只需添加:

location /assets/ {
  alias pathtoyourproject/ROOT/assets/;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM