linux - Nginx 访问服务器上的文件时返回 404 not found

标签 linux ubuntu nginx flask uwsgi

我有一个服务器(Ubuntu 16.04)和一个名为 coxier 的用户。

我将 Nginx 配置为代理请求。我创建了一个文件 etc/nginx/sites-available/myproject

server {
    listen 80;
    server_name 101.200.36.xx;

    location / {
        include uwsgi_params;
        uwsgi_pass unix:/home/coxier/iemoji/server/iemoji.sock;
    }
}

在这个flask项目中,服务器接收请求,然后为这个请求生成一个.gif文件。

一开始我直接用flask#send_file发送1MB左右的gif文件,但是速度很慢。

所以我决定优化请求。

  • 接收http请求并生成gif文件。
  • 将生成的 gif 文件的 url 返回给用户。
  • 用户通过 url 访问 gif 文件。

我有一个问题。 如何生成生成的 gif 的 url?

我试过如下。

server {
    listen 80;
    server_name 101.200.36.xx;

    root /home/coxier/iemoji/server/output;

    location / {
        include uwsgi_params;
        uwsgi_pass unix:/home/coxier/iemoji/server/iemoji.sock;
    }
}

例如,我想访问/home/coxier/iemoji/server/output/a3dfa3eb21daffc7085f71630cbd169e/output.gif

然后我将 http://101.200.36.xx/a3dfa3eb21daffc7085f71630cbd169e/output.gif 返回给用户。

但是 nginx 返回 404 Not Found

最佳答案

来自 https://docs.nginx.com/nginx/admin-guide/web-server/serving-static-content/ ,我找到了解决方案。

server {
    listen 80;
    server_name 101.200.36.xx;


    location / {
        include uwsgi_params;
        uwsgi_pass unix:/home/coxier/iemoji/server/iemoji.sock;
    }

    location ~ \.(gif) {
        root /home/coxier/iemoji/server/output;
        sendfile           on;
        sendfile_max_chunk 1m;
    }
}

您应该在 location block 中重新定义 root。

然后你可以像这样生成url:

http://101.200.36.xx/a3dfa3eb21daffc7085f71630cbd169e/output.gif。

关于linux - Nginx 访问服务器上的文件时返回 404 not found,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49311994/

相关文章:

python - Unicode字符变量导致SyntaxError

regex - 两个php参数的nginx重写规则

ruby-on-rails - Sprockets::Helpers::RailsHelper::AssetPaths::AssetNotPrecompiledError 在应用程序中使用引擎、nginx、乘客

linux - tar 错误 : Unexpected EOF in archive

linux - 在 fish shell 中定义一个别名

Linux 上 .NET Core 中的 C# : Referencing or using . so 文件

amazon-web-services - AWS elastic beanstalk 使用 nginx 位置将 www 重定向到非 www?

linux - shell 脚本 : Year value is not returned

Android Studio 安装 ubuntu 12.04

在 minGW-W64 g++ 中编译的 C++ 代码不能用 Ubuntu g++ 编译