Nginx can access static folder but not other folders inside it. Throws sub-folder/index.html is not found

Viewed 85

I am trying to deploy my django app on google cloud platform. I am using nginx and gunicorn. I am following this guide.

I have created a file - le_website - under the folder sites-available. This is the code -

server {

   listen 80;
   server_name 10.xxx.x.x;
   location = /favicon.ico {access_log off;log_not_found off;} 

   location = /static/ {
     root /home/jainpriyanshu1991/learnEarn/le-webiste;    
   }


   location = / {
     include proxy_params;
     proxy_pass http://unix:/home/jainpriyanshu1991/learnEarn/le-webiste/le_website.sock;
   }
 }

When I try the url myIPaddress/static/ , it works and shows the folders inside it. But it does not work for any subfolder within static. It gives /usr/share/nginx/html/static/img/index.html is not found for img folder inside static. Similarly, when I try the url myIPaddress/ it opens the homepage of website but again, it does not work for any other link and gives error. For about page it gives error /usr/share/nginx/html/about failed (2: No such file or directory).

1 Answers

There is an error in the tutorial you were following. location = ... exactly matches a single URI, whereas you need to match all URIs below /static/.

Use:

location = /favicon.ico { ... } 
location /static/ { ... }
location / { ... }

See this document for details.

Related