【发布时间】:2016-03-11 18:23:19
【问题描述】:
我正在为一个网站设置 nginx,我希望它路由到 3 个位置 - 主前端服务器、api 服务器和 wordpress 博客服务器。我可以让它为前端服务器和 wordpress 工作,但是上游 api 服务器在通过前端访问 API 时总是给出 404。 wordpress 在端口 8080 上运行,而 2 个 NodeJS 服务器在 8015 和 8016 上运行。在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/…你能补充一个答案吗?我会接受它:) 还有为什么它会这样工作