简体   繁体   中英

how to resolve nginx static assets not found error

my application is working fine at

location / {
        include uwsgi_params;
        uwsgi_pass unix:/tmp/socket_file.sock;
}

but when i include static assets location location ~*.\(css|js|jpg|jpeg|gif|png|ico|zip|tgz|gz|rtf) it gives 404 not found error for css,js files

server {
    listen 8080;
    server_name your_domain www.your_domain;
    location ~* \.(css|js|jpg|jpeg|gif|png|ico|zip|tgz|gz|rtf)$ {
       expires max;
       log_not_found off;
       access_log off;
    }

    location / {
        include uwsgi_params;
        uwsgi_pass unix:/tmp/socket_file.sock;
    }
}

nginx error log show `

"/usr/share/nginx/html/static/js/jspdf.js" failed (2: No such file or directory) ' While my static assets are in /ProjectDirectory/assets/

location s are mutually exclusive, ie only one may match at a time. When the first location (regex) matches, directives in the second location / are ignored, and Nginx will proceed with whatever defined outside of this location block (if not defined). In your case, it's apparent that root /usr/share/nginx/html is defined somewhere at an outer level.

A straightforward solution would be to add the same include and uwsgi_pass to the first location block, but since you're serving static files, why not let Nginx handle it by adding root /path/to/your/static/files ?

In practice, instead of matching file extension by regex, it's better to put all your files in one location, and use prefix-based matching:

location /static/ {
    alias /ProjectDirectory/assets/;
}

You might want to read this answer to learn about the differences between root and alias .

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