nginx + uwsgi - 什么是静态文件?

rep*_*cus 5 python nginx uwsgi flask

我使用 nginx 作为几个 Flask 应用程序的代理,使用 uwsgi 作为中间件。这是我的测试应用程序的 nginx 配置。


server {
    listen      80;
    server_name test.myapp.com www.test.myapp.com;
    charset     utf-8;
    client_max_body_size 250M;
    location / { try_files $uri @testapp; }

location @testapp {
    include uwsgi_params;
    uwsgi_pass unix:/tmp/testapp_uwsgi.sock;
}

location /forecaster/components/ {
    alias /opt/test/client/app/components/;
}
Run Code Online (Sandbox Code Playgroud)

}

Run Code Online (Sandbox Code Playgroud)

我很确定 nginx 实际上并没有为静态文件提供服务,即使我注释掉了该location块,文件也是从某些东西中获取的。我在 nginx 日志中看到了 200 个,在 uWsgi 日志中也看到了 200 个。你怎么知道哪个是服务静态文件的?我想烧瓶应用程序也可以为他们服务?

/opt/test/client/app/components/ 肯定存在,并且对其他人可读。有什么方法可以强制 uwsgi 不处理这些请求?

Mic*_*ton 9

你确实需要一个location. 但这不是 nginx 不为您的静态文件提供服务的原因。

问题是您忘记rootserver块中指定指令。所以 nginx 使用它的编译默认值,它依赖于系统,几乎可以肯定不是你的 web 应用程序所在的位置。因此,所有请求,包括对静态文件的请求,都会向上传输到 uWSGI。

要解决此问题,请设置一个root指向应用程序静态资源的指令。

server {
    root /srv/www/testapp;
Run Code Online (Sandbox Code Playgroud)

或者,如果您的子目录中只有静态文件,则可以指定 thenlocationalias ,如 uWSGI 文档中所示

location /static {
    alias /srv/www/testapp/static;
}
Run Code Online (Sandbox Code Playgroud)