gsb-eng
gsb-eng

Reputation: 1209

Nginx 404 File not found error for django static files

I'm getting below error while trying to access static files files, though I'm trying to accessa static/js/main.js file, nginx is looking for static/js/main.js/index.html.. below is the error for the same.

2016/01/20 03:31:01 [error] 2410#0: *1 "/home/ubuntu/vnv/near/near/static/js/main.js/index.html" is not found (20: Not a directory), client: 162.111.50.122, server: my.domain.com, request: "GET /static/js/main.js/ HTTP/1.1", host: "my.domain.com"

Below is my server block..

server {
    listen 80;
    server_name my.domain.com;

    # location ~ ^/(static/.*) {
    location ~ /static {
        root /home/ubuntu/vnv/near/near; # near consist of static directory
    }

    location ~ / {
        proxy_headers_hash_max_size 51200;
        proxy_headers_hash_bucket_size 6400;
        proxy_pass http://near_server;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

Also it is appending / at the end of the URI, what could be the issue here?

Note: I've gone through most of the answers related to this, but I coudn't find proper solution for this situation.

Upvotes: 1

Views: 1449

Answers (1)

Richard Smith
Richard Smith

Reputation: 49812

If the request URI ends in a /, nginx will treat it as a directory and apply the indexing rules. Unless overridden, nginx looks for an index.html file.

From the error message we see that the GET request was for /static/js/main.js/. So the question is who appended the / to the request URI?

If you now look into the access log, you will see a line containing GET /static/js/main.js/ HTTP/1.1 404 which lists the HTTP response code.

If the line above contains GET /static/js/main.js HTTP/1.1 3xx then nginx added the extra /. If there is no such line, then the client asked for the wrong URL and you need to be looking for the solution to the problem elsewhere.

Unrelated to your question: Why are you using regular expression location blocks when prefix location blocks seem more appropriate?

You should probably be using:

location /static { ... }
location / { ... }

Your existing configuration is inefficient and matches URIs with /static embedded anywhere within the string, such as: /something/static.php.

See this document for location syntax and the processing rules.

Upvotes: 1

Related Questions