【问题标题】:nginx upstream configuration always returning 404nginx 上游配置总是返回 404
【发布时间】:2016-03-11 18:23:19
【问题描述】:

我正在为一个网站设置 nginx,我希望它路由到 3 个位置 - 主前端服务器、api 服务器和 wordpress 博客服务器。我可以让它为前端服务器和 wordpress 工作,但是上游 api 服务器在通过前端访问 API 时总是给出 404。 wordpress 在端口 8080 上运行,而 2 个 NodeJS 服务器在 80158016 上运行。在8015 上点击mysite.com 前端服务器时会显示UI,但在端口8016 上调用登录API 时会引发404 错误。 mysite.com/blog 将 URL 改写为 mysite.com:8080 后显示 Worpress 博客

给出了 nginx 配置:

upstream backend {
    server <IP>:8016
}

server {
   listen 80;


  server_name mysite.com;
  location / {

    root /code/public;
    index index.html
    try_files $uri $uri/ /index.html;

  }

  location /api/{
    proxy_set_header Host $http_host;
    proxy_pass http://backend/;
  }

  location /blog {
    root /var/www/html;
    index index.php index.html index.htm;
    try_files $uri $uri/ /index.php;
  }

  location ~\.php$ {
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $remote_addr;
    proxy_set_header Host $host;
    proxy_pass http://<IP>:8080;
  }

  location~/\.ht {
    deny all;
  }
}

这里可能有什么问题?

【问题讨论】:

  • 如果你直接去 :8016 可以吗?如果它不起作用,则不是 Nginx 问题,请检查 Node。
  • 尝试去掉最后的反斜杠location /api {
  • 成功了!!什么小问题。我对这个答案有偏见stackoverflow.com/questions/16157893/…你能补充一个答案吗?我会接受它:) 还有为什么它会这样工作

标签: node.js wordpress nginx


【解决方案1】:

您应该删除尾部斜杠,因为 /api/ 与您的 Node 实例的 /api 不同:

location /api {
  proxy_set_header Host $http_host;
  proxy_pass http://backend/;
}

还要注意这一点:

如果location 由以斜杠字符结尾的前缀字符串定义,并且请求由proxy_pass、fastcgi_pass、uwsgi_pass、scgi_pass 或memcached_pa​​ss 之一处理,则响应URI 等于此的请求字符串,但没有尾部斜杠,带有代码 301 的永久重定向将返回到附加斜杠的请求 URI。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-03-11
    • 2015-11-21
    • 2021-08-05
    • 2015-07-31
    • 2011-06-07
    • 1970-01-01
    • 2020-06-07
    • 2021-07-14
    相关资源
    最近更新 更多