Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 486
Draco Malfago Avatar asked Mar 07 '17 19:03

Draco Malfago


3 Answers

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/ { }
}
like image 161
Draco Malfago Avatar answered Sep 18 '22 12:09

Draco Malfago


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

like image 38
Aleksandar Avatar answered Sep 20 '22 12:09

Aleksandar


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.

like image 25
Vishvajit Pathak Avatar answered Sep 20 '22 12:09

Vishvajit Pathak