【发布时间】:2014-01-07 09:26:13
【问题描述】:
这个项目通常使用 apache,但我想引入 nginx 作为前端控制器来代理请求到 memcached,或者如果在 memcached 中找不到 URI 作为键,则回退到 apache。
当我通过 nginx 发出请求时,我在每个资产上都得到 404。我可以将来自请求的单个资产 URL 粘贴到 URL 栏中并检索它,但状态为 404。 404 导致大部分页面无法呈现,但似乎正在下载资产。
我可以直接通过 apache 发出相同的请求,而且效果很好。
这是我的 nginx 配置:
upstream memcached-upstream {
server 127.0.0.1:11211;
}
upstream apache-upstream {
server 127.0.0.1:5678;
}
server {
listen 4567;
root /vagrant;
server_name sc;
index index.php;
access_log /var/log/nginx/www.sc.com.access.log;
error_log /var/log/nginx/www.sc.com.error.log error;
location / {
# Only use this method for GET requests.
if ($request_method != GET ) {
proxy_pass http://apache-upstream;
break;
}
# Attempt to fetch from memcache. Instead of 404ing, use the @fallback internal location
set $memcached_key $request_uri;
memcached_pass memcached-upstream; # Use an upstream { } block for memcached resiliency
default_type application/json; # Our services only speak JSON
error_page 404 = @fallback;
}
location @fallback {
proxy_pass http://apache-upstream;
}
}
这是我的 nginx 访问日志中的一个示例:
10.0.2.2 - - [18/Dec/2013:23:50:08 +0000] "GET /templates/assets/js/csrf.js HTTP/1.1" 404 545 "http://localhost:4567/templates/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36"
还有来自 apache 日志的相同请求:
www.sc.com:80 127.0.0.1 - - [18/Dec/2013:23:50:08 +0000] "GET /templates/assets/js/csrf.js HTTP/1.0" 200 857 "http://localhost:4567/templates/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36"
任何帮助将不胜感激。
【问题讨论】:
标签: apache nginx memcached http-status-code-404 backend