1

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/

AzamAbbasi
  • 91
  • 1
  • 9

1 Answers1

3

locations are mutually exclusive, i.e. 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.

iBug
  • 35,554
  • 7
  • 89
  • 134