嘿伙计们,过去一天我一直在努力解决这个问题,我需要一些帮助。
所以本质上我在主域和子域中使用 nginx 存储了两个图像。
http://orgasmal.com/r.png(旧金山)
http://assets.orgasmal.com/e.gif(旧金山)
它们在浏览器中都能很好地加载,这很好。唯一的问题是,当我 curl 主域时,它返回 404。
curl -I http://orgasmal.com/r.png
HTTP/1.1 404 Not Found
Server: nginx/1.6.3
Date: Mon, 29 Aug 2016 18:52:07 GMT
Content-Type: text/html
Content-Length: 168
Connection: keep-alive
但是当我卷曲子域时,它可以按预期工作。
curl -I http://assets.orgasmal.com/e.gif
HTTP/1.1 200 OK
Server: nginx/1.6.3
Date: Mon, 29 Aug 2016 18:53:05 GMT
Content-Type: image/gif
Content-Length: 145
Last-Modified: Mon, 29 Aug 2016 05:34:06 GMT
Connection: keep-alive
ETag: "57c3c94e-91"
Accept-Ranges: bytes
我几乎尝试了所有方法来实现这个功能,即使我的 .conf 文件中的服务器块相同,但主域图像仍然会显示 404。如能得到任何帮助,我将不胜感激。
server {
listen 80;
server_name orgasmal.com;
# note that these lines are originally from the "location /" block
root /var/www/orgasm;
index index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
server {
listen 80;
server_name assets.orgasmal.com;
# note that these lines are originally from the "location /" block
root /var/www/img;
index index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
目录也相同
drwxr-xr-x 2 nginx nginx 4096 Jul 18 11:30 cgi-bin
drwxr-xr-x 2 nginx nginx 4096 Jul 18 11:30 html
drwxr-xr-x 2 nginx nginx 4096 Aug 29 14:00 img
drwxr-xr-x 2 nginx nginx 4096 Aug 29 14:00 orgasm
编辑
我查看了我的 nginx 错误日志,这似乎与我遇到的问题有关。403 错误是预期的,但上面的两个错误似乎是某种奇怪的重定向,搞乱了一切。不确定如何修复它,但这似乎是问题所在。
2016/08/29 16:23:35 [error] 28032#0: *53 open() "/usr/share/nginx/html/r.png" failed (2: No such file or directory), client: MY IP, server: _, request: "HEAD /r.png HTTP/1.1", host: "orgasmal.com"
2016/08/29 16:24:01 [error] 28032#0: *54 open() "/usr/share/nginx/html/r.png" failed (2: No such file or directory), client: MY IP, server: _, request: "HEAD /r.png HTTP/1.1", host: "orgasmal.com"
2016/08/29 16:24:48 [error] 28032#0: *55 directory index of "/var/www/orgasm/" is forbidden, client: MY IP, server: orgasmal.com, request: "GET / HTTP/1.1", host: "orgasmal.com"
答案1
怀疑是您的客户端,因为当我从我的 AWS 服务器 curl 时它工作正常。
curl -I http://orgasmal.com/r.png
HTTP/1.1 200 OK
Server: nginx/1.6.3
Date: Mon, 29 Aug 2016 19:53:45 GMT
Content-Type: image/png
Content-Length: 224603
Last-Modified: Mon, 29 Aug 2016 17:41:59 GMT
Connection: keep-alive
ETag: "57c473e7-36d5b"
Accept-Ranges: bytes
它也可以很好地工作网页测试- 点击查看结果。
子域名 curl 也很好用。不过我并不想在我使用的电脑上点击这个链接。
提供日志后更新
关于这部分配置
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
error_page 指令表示“如果出现 500 错误,则查找 50x.html 页面”。位置规则表示“不要查找静态 html 页面,而是查找 /usr/share/nginx/htm 目录中请求的文件”。此日志条目确认了正在发生的事情。
"/usr/share/nginx/html/r.png" failed (2: No such file or directory)
这是相当奇怪的配置。我会将其完全删除,只保留 error_page 指令和位置,并让服务器在发现错误时发回适当的错误代码。
答案2
问题是什么
/etc/nginx/nginx.conf 中的这个服务器块有点混乱,所以我删除了它,现在一切都正常了。
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
来自我的客户
curl -I http://orgasmal.com/r.png
HTTP/1.1 200 OK
Server: nginx/1.6.3
Date: Mon, 29 Aug 2016 21:11:53 GMT
Content-Type: image/png
Content-Length: 224603
Last-Modified: Mon, 29 Aug 2016 17:41:59 GMT
Connection: keep-alive
ETag: "57c473e7-36d5b"
Accept-Ranges: bytes