Score:0

Nginx giving "No such file or directory" error for static files

by flag

I am trying to deploy a site on to a linux server following Corey Schafer's guide for Flask.

My site is working, but nginx is unable to read/access/find the static folder and the files within.

I initially removed the default sites-enabled and created a custom one:

sudo rm /etc/nginx/sites-enabled/default

Then:

sudo nano /etc/nginx/sites-enabled/flaskblog

This is what I input into that file:

server {
    listen 80;
    server_name DOMAIN_IP;

    location /static {
        alias /Users/myname/VScode/Flask_Blog/flaskblog/static;
    }

    location / {
        proxy_pass http://localhost:8000;
        include /etc/nginx/proxy_params;
        proxy_redirect off;
    }
}

But all static pages return 404

If I check the error log of nginx I see:

   2022/06/03 06:09:42 [error] 71064#71064: *1 open() "/Users/myname/VScode/Flask_Blog/flaskblog/static/main.css" failed (2: No such file or directory), client: 217.146.83.247, server: IP, request: "GET /static/main.css HTTP/1.1", host: "IP", referrer: "http://IP/"
2022/06/03 06:09:42 [error] 71064#71064: *1 open() "/Users/myname/VScode/Flask_Blog/flaskblog/static/profile_pics/271fcf0ffe2a92aa.jpg" failed (2: No such file or directory), client: 217.146.83.247, server: IP, request: "GET /static/profile_pics/271fcf0ffe2a92aa.jpg HTTP/1.1", host: "IP", referrer: "http://IP/"
2022/06/03 06:09:42 [error] 71064#71064: *4 open() "/Users/myname/VScode/Flask_Blog/flaskblog/static/profile_pics/default.jpg" failed (2: No such file or directory), client: 217.146.83.247, server: IP, request: "GET /static/profile_pics/default.jpg HTTP/1.1", host: "IP", referrer: "http://IP/"
2022/06/03 06:09:45 [error] 71064#71064: *4 open() "/Users/myname/VScode/Flask_Blog/flaskblog/static/main.css" failed (2: No such file or directory), client: 217.146.83.247, server: IP, request: "GET /static/main.css HTTP/1.1", host: "IP", referrer: "http://IP/home"

So I can see that nginx is going to the correct location, so I don't understand why it is saying "No such file or directory"

I have tried both

location /static {
        alias /Users/myname/VScode/Flask_Blog/flaskblog/static;
    }

and

location /static {
        root /Users/myname/VScode/Flask_Blog/flaskblog;
    }

And both produce the same error and same location.

I have also cd'd down and then done ls at each point to check that everything is in the right place and everything seems in order

(venv) charles@flask-server:~/Flask_Blog$ ls
flaskblog  __pycache__  requirements.txt  run.py  venv
(venv) charles@flask-server:~/Flask_Blog$ cd flaskblog
(venv) charles@flask-server:~/Flask_Blog/flaskblog$ ls
config.py  errors  __init__.py  main  models.py  posts  __pycache__  site.db  static  templates  users
(venv) charles@flask-server:~/Flask_Blog/flaskblog$ cd static
(venv) charles@flask-server:~/Flask_Blog/flaskblog/static$ ls
main.css  profile_pics
(venv) charles@flask-server:~/Flask_Blog/flaskblog/static$ cd main.css
-bash: cd: main.css: Not a directory
(venv) charles@flask-server:~/Flask_Blog/flaskblog/static$ cd profile_pics
(venv) charles@flask-server:~/Flask_Blog/flaskblog/static/profile_pics$ ls

And if I go to "/Users/myname/VScode/Flask_Blog/flaskblog/static" on my personal machine it goes to the correct place.

Thank you for any help.

Score:0
ph flag
Flo

Your question is missing some information about the directory permissions of /Users/myname/VScode/Flask_Blog/. The default user statement in your nginx.conf should define something like user www-data;.

So this user need to have appropriate permissions to your static folder. You could do

chown -R www-data: /Users/myname/VScode/Flask_Blog/flaskblog/static

by flag
Thank you. Should I input that into the terminal? I tried inputting it into the terminal and got the same error. ```chown: cannot access '/Users/myname/VScode/Flask_Blog/flaskblog/static': No such file or directory```
by flag
I also tried going to the folder on my machine, right clicking, clicking "get info" copying the location straight from there and then doing ```ls /Users/myname/VScode/Flask_Blog/flaskblog/static/main.css``` pasting the location directly in and I also get ```No such file or directory```
Flo avatar
ph flag
Flo
@acodeaday to help you with that, we need more information about your OS. Which one are you running? how is your environment set up?
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.