Nginx lost my static images for one site in sites-

2019-09-13 03:17发布

问题:

I have two sites in sites-enabled for nginx:

1) project - this is essentially the top level domain - mysite.com

server {
    listen 80;
    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-deploy/project/static/;
    }
}

2) blog - this is for a blog, that is accessible via: mysite.com:8080

server {
    listen 8080;

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

Nginx has accepted both configurations, but when I visit 1 i get all of the text, but no css, or images.

If I visit 2 i get everything working fine.

What am I doing wrong?

Thank you

回答1:

For the alias directive to work correctly, the URI in the location and the URI in the alias directive should both end in / or neither end in /. The algorithm seems to substitute one string for the other.

So you should probably write:

location /static {
    alias  /home/www/flask-deploy/project/static;
}

However, when the last element(s) of the alias match the location, the root directive is preferred (see this):

location /static {
    root  /home/www/flask-deploy/project;
}

Your location blog/ looks incorrect, prefix locations should always begin with a /.