【问题标题】:Nginx can access static folder but not other folders inside it. Throws sub-folder/index.html is not foundNginx 无法访问静态文件夹,但不能访问其中的其他文件夹。找不到子文件夹/index.html
【发布时间】:2019-10-18 03:37:36
【问题描述】:

我正在尝试在谷歌云平台上部署我的 django 应用程序。我正在使用 nginx 和 gunicorn。我关注this guide

我创建了一个文件 - le_website - 在文件夹可用的站点下。这是代码 -

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

当我尝试使用 url myIPaddress/static/ 时,它可以工作并显示其中的文件夹。但它不适用于静态中的任何子文件夹。它给出/usr/share/nginx/html/static/img/index.html is not found for img folder inside static。同样,当我尝试使用 url myIPaddress/ 时,它会打开网站的主页,但同样,它不适用于任何其他链接并给出错误。对于 about 页面,它给出错误 /usr/share/nginx/html/about failed (2: No such file or directory)。

【问题讨论】:

  • 您的意思是在最后两个location 块中使用= 修饰符吗?试试看:location /static/ { ... } location / { ... }
  • @RichardSmith 您能否添加您的评论作为答案,以便让社区的其他成员看到?谢谢。

标签: django nginx google-cloud-platform gunicorn


【解决方案1】:

您所遵循的教程中存在错误。 location = ... 完全匹配单个 URI,而您需要匹配 /static/ 以下的所有 URI。

用途:

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

详情请见this document

【讨论】:

    猜你喜欢
    • 2012-05-30
    • 2021-07-22
    • 2021-04-05
    • 1970-01-01
    • 1970-01-01
    • 2016-08-03
    • 1970-01-01
    • 2014-02-09
    • 1970-01-01
    相关资源
    最近更新 更多