nginx and nodejs serve images from dynamic folder

冷暖自知 提交于 2021-01-29 03:14:22

问题


Hi i have this nginx setup:

server {
listen 80;
server_name xxxxxx.xx www.xxxxxxx.xx;
root /home/xxxx/public_html;

location / {
    proxy_pass http://127.0.0.1:3000;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection $connection_upgrade;
    proxy_http_version 1.1;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $host;
    #proxy_redirect off; 
    #proxy_cache_bypass $http_upgrade;
  }
}

My app creates folders inside the uploads/user followed by the user's ID and then stores images into it. Every user have his own folder inside uploads/user/xxxx.

ex.

/uploads/user/5878db663e67e3535a47c638/1085.github-logo.png

The app works fine in browser but when the user uploads the image it cannot be displayed so a 404 rises.

what can i do?


回答1:


You can use try_files to serve up static content (if it exists) and otherwise defer to NodeJS:

root /home/xxxx/public_html;

location / {
    try_files $uri $uri/ @proxy;
}
location @proxy {
    proxy_pass http://127.0.0.1:3000;
    ...
}

See this document for details.



来源:https://stackoverflow.com/questions/41649477/nginx-and-nodejs-serve-images-from-dynamic-folder

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!