简体   繁体   中英

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. However, how can I tell if nginx is actually serving the static files or not? I tried changing the alias /home/pi/Public/flaskapp/static/; to .../static-testing/; and just put a placeholder style.css there but the page seems to load like before.

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. 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! I also changed the listening port to be 80 (suprise).

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.

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.

您可以将自定义标头添加到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

You should add this into your nginx conf in the http block

upstream backend {
  server 127.0.0.0.1:8000;
}

Then change the proxy_pass to 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. you will see '-' when nginx does not request the upstream.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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