我最近把我的CentOS 7机器上的Web服务器从Apache切换到了Nginx。 我使用软件在我的计算机上截取屏幕截图并将其上传到服务器供公众查看。 最初,它在Apache上运行良好 – 但是,现在我遇到了一个问题,在这个问题上,nginx将为web服务器上的一些.png图像提供404错误。
我提到了一个非常类似的问题,但是在我自己的问题的背景下,我没有办法解决这个问题。
我有一个下面的图像服务器的服务器块的片段。
server { listen 80; server_name imageserver.example.com; root /usr/share/nginx/imageserver.example.com/public_html; access_log imageserver.example.com/logs/imageserver.example.com_access.log; error_log imageserver.example.com/logs/imageserver.example.com_error.log crit; location / { index index.html index.htm; } location ~* \.png { root imageserver.example.com/public_html/u/; expires max; add_header Pragma public; add_header Cache-control "public, must revalidate, proxy-revalidate"; } }
一个示例链接是这个图像 。 我已经确认该文件存在于服务器上,并具有以下权限。
$ lsa 7b724394b851299c68b15f1172f158c6.png -rw-rw-r--. 1 jflory nginx 2.4K Nov 3 11:13 7b724394b851299c68b15f1172f158c6.png
我很困惑如何解决这个问题。 问题是什么?
location ~* \.png容器中的root指令在URL的末尾添加/ u /,所以服务器正在/ u / u /中查找图像。
这个root指令实际上是不必要的,因为这个值将从您的server块中定义的值inheritance。
映像文件位于根目录中,而不是/ u /
删除/ u在你的链接,你会看到图像加载。
奇怪的是,不会/是图像的根目录,因为这是他们都存储的地方? 我不知道我是不是很明白为什么这个修好了。
您只需要提供public_html目录的path。 看下面的例子来理解。
文件结构:
.../public_html/ -- /u/myimg.jpg -- /index.html
在index.html显示图片的代码:
<img src="/u/myimg.jpg">
所以,如果你设置.../public_html/u/作为你的映像服务器path,那么服务器将尝试find.../public_html/u/u/myimg.jpg ,所以它会抛出404错误。 希望这可以帮助 :)