[英]404 on POST and GET requests for Node.js app behind NGINX reverse proxy
I have an Ubuntu web server running with this structure:我有一个以这种结构运行的 Ubuntu Web 服务器:
Nginx reverse proxy localhost:80, which redirects to either '/'
(apache server with WordPress site at localhost:8080), which currenly works. Nginx 反向代理 localhost:80,它重定向到
'/'
(带有 WordPress 站点的 apache 服务器,位于 localhost:8080),目前有效。
More recently a I've tried to add a Node.js Application at www.site.com/app
or, internally, localhost:3000
.最近,我尝试在
www.site.com/app
或在内部添加 Node.js 应用程序localhost:3000
。 I am able so serve the HTML and CSS of the node.js webapp, however all internal route calls at 404ing, likely because of the URL addressing of /app/.我能够提供 node.js webapp 的 HTML 和 CSS,但是所有内部路由调用都在 404ing,可能是因为 /app/ 的 URL 寻址。
IE. IE。 Tries to hit
/someendpoint
and 404s because Node.js is technically running on localhost:3000
( www.site.com/app
).尝试点击
/someendpoint
和/someendpoint
,因为 Node.js 在技术上运行在localhost:3000
( www.site.com/app
) 上。 Should I be routing arguments like ( www.site.com/app/someendpoint
)?我应该像 (
www.site.com/app/someendpoint
) 这样的路由参数吗?
The Problem: All POST/GET calls from NODE.JS are 404ing because of my bad understanding of NGINX config.
问题:所有来自 NODE.JS 的 POST/GET 调用都是 404ing,因为我对 NGINX 配置的理解不好。 How do I route this GET calls to the actual location of the Node.js server which is (site.com/app/, localhost:3000).
如何将此 GET 调用路由到 Node.js 服务器的实际位置,即 (site.com/app/, localhost:3000)。
Here is my ' default
' config from /etc/nginx/available_sites/.
这是我来自
/etc/nginx/available_sites/.
“ default
”配置/etc/nginx/available_sites/.
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
index index.php index.html index.htm index.nginx-debian.html;
server_name www.*site*.name;
location / {
proxy_pass http://127.0.0.1:8080$request_uri;
proxy_buffering on;
proxy_buffers 12 12k;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $host;
try_files $uri $uri/ /index.php?q=$uri&$args;
}
#Currenly serving HTML, CSS of site, however node routes 404
location /app/ {
proxy_pass http://localhost:3000/;
}
}
How might I update this NGINX config file to account for node endpoints actively trying to hit the route of my apache site and not the /app
real location of the node server?我如何更新此 NGINX 配置文件以考虑主动尝试访问我的 apache 站点的路由而不是节点服务器的
/app
实际位置的节点端点?
Any help or ideas would be great, I've been stuck on this issue for a while as part of a personal project.任何帮助或想法都会很棒,作为个人项目的一部分,我已经在这个问题上停留了一段时间。
Thanks!谢谢!
Here is a sample app.js file that may offer you some insight into the matter.这是一个示例 app.js 文件,可以让您深入了解这个问题。
var express = require("express");
var router = require("./lib/routes/index");
var app = express();
var port = 3000;
app.use('/app', router);
app.listen(port, function () {
console.log("Listening on port " + port);
});
As for the nginx configuration, I would recommend something along the lines of the following:至于 nginx 配置,我会推荐以下内容:
# Sample nginx config with 2 upstream blocks
upstream nodeApp {
server 127.0.0.1:3000;
}
upstream apacheApp {
server 127.0.0.1:8080
}
server {
listen 80;
listen [::]:80;
server_name www.domain.com domain.com;
root /var/www/domain;
location / {
proxy_pass http://apacheApp;
}
location /app {
proxy_pass http://nodeApp;
# OR
# try_files $uri $uri/ @backend;
}
location @backend {
proxy_pass http://nodeApp;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
}
The key part of this is using an external router in your app.js, then using this line: app.use('/app', router);
关键部分是在 app.js 中使用外部路由器,然后使用这一行:
app.use('/app', router);
You may want to also set up nginx to serve static files instead of relying on express.static()
.您可能还想设置 nginx 来提供静态文件,而不是依赖
express.static()
。 This would also be easy to do by setting up more location blocks like so:这也很容易通过设置更多的位置块来实现,如下所示:
location /app/public {
try_files $uri $uri/ =404;
}
This should work for your purposes.这应该适用于您的目的。 Don't forget to check your configuration with
nginx -t
.不要忘记使用
nginx -t
检查您的配置。
please remove the try_files statement in the location / block your location should look like this .....请删除位置/块中的 try_files 语句,您的位置应如下所示.....
location / {
proxy_pass http://localhost:8080;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
For more troubleshooting advice, check out this very similar thread: nginx proxy_pass 404 error, don't understand why有关更多故障排除建议,请查看此非常相似的线程: nginx proxy_pass 404 错误,不明白为什么
The solution that worked with my 404 issue was to add an extra /
after my proxy_pass url.处理我的 404 问题的解决方案是在我的 proxy_pass url 之后添加一个额外的
/
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.