对于 nginx 来说简单,对于 express 来说复杂

对于 nginx 来说简单,对于 express 来说复杂
server {
    listen        80;
    server_name  mydomain.com;
    location /sublocation/ {
        proxy_pass http://localhost:8090;
        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;
    }
    location / {
        proxy_pass         http://localhost:5000;
        proxy_http_version 1.1;
        proxy_set_header   Upgrade $http_upgrade;
        proxy_set_header   Connection keep-alive;
        proxy_set_header   Host $host;
        proxy_cache_bypass $http_upgrade;
        proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header   X-Forwarded-Proto $scheme;
    }
}

在这里,我使用了两个不同的位置来处理服务器中不同应用程序的请求,但在访问时仍然出现 404 错误http://mydomain.com/sublocation/

但我可以使用http://mydomain.com:8090/

我不知道问题出在哪里。请提供任何建议。

答案1

问题是该路径可能仍然存在/sublocation于查询中。

对于 nginx 来说简单,对于 express 来说复杂

尝试:

    location /sublocation {
        proxy_pass http://localhost:3040/sublocation;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_set_header  X-Real-IP $remote_addr;
        proxy_set_header  X-Forwarded-Proto https;
        proxy_set_header  X-Forwarded-For $remote_addr;
        proxy_set_header  X-Forwarded-Host $remote_addr;
        proxy_cache_bypass $http_upgrade;
    }

但是在你的 express 应用程序中你必须/sublocation以 root 身份输入!你可以这样做:

    var mainApp = express();
    mainApp.use(bodyParser.urlencoded({extended:true, limit: '50mb'}));
    //...
    var app = express(); // your current app
    mainApp.use('/sublocation',be.app); // here is the magic
    //rest of your app
    

相关内容