我有一个服务器(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/