简体   繁体   English

验证Nginx是否正在提供静态文件而不是Flask

[英]Verify that Nginx is serving static files instead of Flask

I have a flask app running gunicorn -w 1 -b 0.0.0.0:8000 flaskapp:app with below nginx config. 我有一个烧瓶应用程序运行gunicorn -w 1 -b 0.0.0.0:8000 flaskapp:app与下面的nginx配置。 However, how can I tell if nginx is actually serving the static files or not? 但是,如何判断nginx是否实际提供静态文件呢? I tried changing the alias /home/pi/Public/flaskapp/static/; 我尝试更改alias /home/pi/Public/flaskapp/static/; to .../static-testing/; to .../static-testing/; and just put a placeholder style.css there but the page seems to load like before. 并在那里放置一个占位符style.css ,但页面似乎像以前一样加载。

server {
    listen 5000;
    server_name _;
    location / {
        proxy_pass http://127.0.0.0.1:8000;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    }
    location /static {
        alias  /home/pi/Public/flaskapp/static/;
    }
}

Am I missing something obvious? 我错过了一些明显的东西吗 does one have to specify something in the routes of flask? 是否必须在烧瓶路线中指明一些东西?

So I finally configured the nginx properly. 所以我最终正确配置了nginx。 I added root and removed hard path of static, also added log-files that clearly shows that static and css is being loaded from nginx! 我添加了root并删除了静态的硬路径,还添加了日志文件,清楚地显示静态和css正在从nginx加载! I also changed the listening port to be 80 (suprise). 我还将监听端口改为80(惊讶)。

server {
    listen 80;

    server_name myapp.com;
    root /home/pi/Public/myapp;

    access_log /home/pi/Public/myapp/logs/nginx-access.log;
    error_log /home/pi/Public/myapp/logs/nginx-error.log;

    location / {
        proxy_pass http://127.0.0.1:8000;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    }

    location /static/ { }

    location /uploads/ { }
}

You can probably test this with empty path mentioned in the /static/ location. 您可以使用/ static / location中提到的空路径来测试它。

server {
    listen 5000;
    server_name _;

    location /static/ {

    }

    location / {
        proxy_pass http://127.0.0.0.1:8000;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    }

} }

This will give 404 error and thus you can verify if files are served by Nginx. 这将给出404错误,因此您可以验证文件是否由Nginx提供。

您可以将自定义标头添加到nginx位置块,并查看它是否在静态文件上设置。

I think the easiest way is to log some upstream variables into the access log. 我认为最简单的方法是将一些上游变量记录到访问日志中。

http://nginx.org/en/docs/http/ngx_http_upstream_module.html#variables http://nginx.org/en/docs/http/ngx_http_upstream_module.html#variables

You should add this into your nginx conf in the http block 您应该将它添加到http块中的nginx conf中

upstream backend {
  server 127.0.0.0.1:8000;
}

Then change the proxy_pass to http://backend ; 然后将proxy_pass更改为http:// backend ;

Now add 现在添加

log_format upstream '$upstream_bytes_received $upstream_response_time';
access_log /var/log/nginx-upstream upstream;

to your server block and restart nginx. 到您的服务器阻止并重新启动nginx。 you will see '-' when nginx does not request the upstream. 当nginx不请求上游时,你会看到' - '。

Doc: http://nginx.org/en/docs/http/ngx_http_log_module.html & http://nginx.org/en/docs/http/ngx_http_upstream_module.html Doc: http//nginx.org/en/docs/http/ngx_http_log_module.html&http : //nginx.org/en/docs/http/ngx_http_upstream_module.html

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

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