【发布时间】:2013-12-21 04:56:02
【问题描述】:
我当前的 nginx 配置有问题。我想做的是:
- 对于没有任何路径的请求,获取 index.html(有效)
- 直接获取现有文件(有效)
- 如果请求的文件或路径在物理上不存在,则代理请求到 nodejs (404)
我已经尝试过在 stackoverflow 上找到的几种配置,但没有一种适合我的需求。
这是我目前的配置:
# IP which nodejs is running on
upstream app_x {
server 127.0.0.1:3000;
}
# nginx server instance
server {
listen 80;
server_name x.x.x.x;
#access_log /var/log/nginx/x.log;
root /var/www/x/public;
location / {
root /var/www/x/public;
index index.html index.htm index.php;
}
location ^/(.*)$ {
if (-f $request_filename) {
break;
}
proxy_set_header Host $http_host;
proxy_pass http://127.0.0.1:3000;
}
}
【问题讨论】:
-
只需删除
if (-f $request_filename)语句和中断?我真的不知道什么有效,什么无效,以及您的需求是什么。 -
您没有为
index.html提供节点服务有什么原因吗?这会让事情变得更容易。 -
我不使用 nodejs 提供 index.html 的原因是,它是静态 html。该项目是一个 ajax 驱动的应用程序,我试图用 nodejs 只提供 json。
-
上游不使用有什么用?