访问服务器上的文件时找不到 Nginx returns 404

Nginx returns 404 not found when access file on server

我有一个服务器(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文件,但是速度很慢。

所以我决定优化请求。

我有一个问题。 如何生成生成的 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.

然后我returnhttp://101.200.36.xx/a3dfa3eb21daffc7085f71630cbd169e/output.gif给用户

但是nginx returns 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 块中重新定义 root。

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

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