【问题标题】:Nginx and node.js routes issueNginx 和 node.js 路由问题
【发布时间】:2013-10-14 08:24:00
【问题描述】:

我已经设置了 nginx 来为我的节点网络应用程序(api + web)提供服务,但我看到服务器只响应“/”(网络根)调用。 当我测试它时,我看到了主网页(位于 /index.html),但没有图像或 css 样式,还有路由 /api/v1/....(/api/v1/users, /api/v1/cars 等)无法访问,因为 nginx 响应“未找到”。

当前 nginx 配置为:

server {
    listen 80;
    server_name localhost www.mydomain.com mydomain.com

    access_log off;
    error_log off;

    location = / {
        proxy_pass http://127.0.0.1:3000;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Real-IP $remote_addr;
    }
}

如何配置 nginx 以便为所有路由提供服务?

【问题讨论】:

    标签: node.js api nginx


    【解决方案1】:

    要匹配所有路由,请删除 = 符号。带有 = 前缀的指令将完全匹配查询。更多信息可以在here找到。

    location / {
      proxy_pass http://127.0.0.1:3000;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header X-Real-IP $remote_addr;
    }
    

    以下是文档中的示例:

    location  = / {
      # matches the query / only.
      [ configuration A ] 
    }
    location  / {
      # matches any query, since all queries begin with /, but regular
      # expressions and any longer conventional blocks will be
      # matched first.
      [ configuration B ] 
    }
    

    【讨论】:

    • 拯救了我的一天!花了几个小时解决这个问题......谢谢!
    猜你喜欢
    • 1970-01-01
    • 2019-09-02
    • 2021-04-17
    • 2018-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-04
    • 1970-01-01
    相关资源
    最近更新 更多