Nginx 对静态文件给出“没有这样的文件或目录”错误

Nginx 对静态文件给出“没有这样的文件或目录”错误

我正在尝试按照 Corey Schafer 的 Flask 指南将站点部署到 Linux 服务器上。

我的网站正在运行,但是 nginx 无法读取/访问/查找静态文件夹及其中的文件。

我最初删除了默认的站点启用功能并创建了一个自定义的站点启用功能:

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

然后:

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

这是我输入到该文件中的内容:

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;
    }
}

但所有静态页面都返回404

如果我检查 nginx 的错误日志,我会看到:

   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"

所以我可以看到 nginx 正在前往正确的位置,所以我不明白为什么它说“没有这样的文件或目录”

我已经尝试过了

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

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

并且两者都产生相同的错误和相同的位置。

我还对每一点进行了检查,ls以确保一切都在正确的位置,一切似乎都井然有序

(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

如果我在个人机器上访问“/Users/myname/VScode/Flask_Blog/flaskblog/static”,它会转到正确的位置。

感谢您的任何帮助。

相关内容