【问题标题】:Why do my POST requests not get proxy_passed by nginx to my nodejs app?为什么我的 POST 请求没有被 nginx 代理传递到我的 nodejs 应用程序?
【发布时间】:2023-03-13 22:33:01
【问题描述】:

当我尝试从我的 Node.js 应用程序 POST 到“/”时,我收到“/home/”的目录索引被禁止,但我不确定为什么 nginx 甚至尝试直接处理该请求?也许我误解了日志的输出。一些额外的调试表明 POST 根本没有对我的应用程序进行。

nginx 配置:

upstream appname {
    sever localhost:3000;
}

server {
    listen 80;
    error_log /etc/nginx/debug.log;
    rewrite_log on;
    root /home/;
    sever_name mydomain.name;

    location /appname/ {
        proxy_redirect off;
        proxy_pass http://appname/;
    }
}

如果我将我的位置设为“/”,它当然可以完美运行。

在节点中它甚至没有达到

app.post('/', function(req, res){

但是

app.get('/', function(req, res){
    res.sendFile('index.html', {root: path.join(__dirname, '../views')})
});

按预期工作并返回正确的资源。

为什么我的 POST 请求(似乎)没有通过代理?

【问题讨论】:

    标签: javascript node.js nginx


    【解决方案1】:

    看起来您的 nodejs 应用程序在 POST 之后重定向到 /home/,但不知道它已被代理并使用 /appname/ 调用,您应该让您的应用程序了解命名空间。并使其正确重定向到/appname/home/

    【讨论】:

    • 所以这在技术上是正确的,但正如我所接受的那样,让我解释一下为什么人们会偶然发现这篇文章。我所有的资源和 ajax 请求都像 '$.ajax url: "/"' 或 '' 前导 / 将请求定向到nginx 中指定的根目录。当一切都在我的客户端 js 中时,我遇到的问题是试图在节点应用程序中修复它。
    猜你喜欢
    • 2011-10-03
    • 1970-01-01
    • 2021-06-30
    • 2021-09-09
    • 2019-08-30
    • 1970-01-01
    • 2020-10-29
    • 1970-01-01
    • 2012-06-30
    相关资源
    最近更新 更多