简体   繁体   中英

Confirm if application is using nginx to serve static files

I am using this tutorial - part 1 , but i am not sure how to test if the app is running with nginx serving static files or not.

I have exactly the same code.


/etc/nginx/sites-available/flask_project

server {
    location / {
        proxy_pass http://localhost:8000;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    }
    location /static {
        alias  /home/www/flask_project/static/;
    }
}

And then:

gunicorn app:app -b localhost:8000

All routes are working fine. However if I do http://localhost:8000/static i will see

Not Found

The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again.

And apparently I should see the page with <h1>Test!</h1> from static folder.

What i am doing wrong?

Basically i want to know how configure nginx to serve static files and then confirm.

-app.py
-static
   -index.html

First, requests to port 8000 completely bypass nginx, so nothing strange here. You should go to localhost without port number.

Second, you have to symlink this config to /etc/nginx/sites-enabled and reload nginx.

Third, your static location is wrong. You have location without trailing slash and alias with one. They should always be with or without trailing slash simultaneously. And in this case it's even better to have root directive.

server {
    root /home/www/flask_project;
    index index.html;

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

    location /static/ {
        # empty. Will serve static files from ROOT/static.
    }
}

You should make request directly http://localhost:8000/static/index.html then you will see response.

But if you want to see on index.html by default, you should have something like in conf:

location /static {
    alias  /home/www/flask_project/static/;
    try_files $uri $uri/index.html index.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