大网数据广告活动
服务器运维问答

Receiving a 404 for a file that exists in nginx web server

· 52CE 我爱测

I recently switched the web server on my CentOS 7 machine from Apache to nginx. I use software to take screenshots on my computers and to upload them to the server for public viewing. Originally, it worked fine on Apache – howeve…

问题原文

原帖链接 · J.W.F. · CC BY-SA 3.0

I recently switched the web server on my CentOS 7 machine from Apache to nginx. I use software to take screenshots on my computers and to upload them to the server for public viewing. Originally, it worked fine on Apache – however, now I am running into an issue where nginx will serve a 404 error for some of my .png images on the web server.

I referred to this question with a very similar issue, but I couldn't make sense of the solution in the context of my own problem.

I have a snippet of the server block for the image server below.

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";
    }
}

An example link is this image. I have confirmed the file exists on the server, and it has the following permissions.

$ lsa 7b724394b851299c68b15f1172f158c6.png -rw-rw-r--. 1 jflory nginx 2.4K Nov 3 11:13 7b724394b851299c68b15f1172f158c6.png

I am very confused about how to fix this. What could the problem be?

已采纳解答

原帖链接 · Richard Smith · CC BY-SA 3.0

The root directive in the location ~* \.png container is adding a /u/ to the end of your URL, so the server is looking in /u/u/ for the image.

This root directive is actually unnecessary, as the value will be inherited from the value defined in your server block.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2015-11-05)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。