繁体   English   中英

无法使用 Nginx 反向代理节点后端

[英]Can't reverse proxy Node backend using Nginx

我在 AWS EC2 上使用 Angular11 + NodeJS。

我正在尝试使用 nginx 设置反向代理。

nginx:

server {
    listen 80;
    listen [::]:80;
    server_name http://MY-AWS-IP.com;
    root /home/ubuntu/dashboard/frontend/dist/frontangular;
    server_tokens off;
    index index.html index.htm;

location /auth/ {
        proxy_pass http://localhost:3000;
        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 /groceries/ {
        proxy_pass http://localhost:3000;
        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;
    }

}

节点:(在 pm2 上运行)

... Some stuff
const ports = process.env.PORT || 3000;

app.use("/groceries", groceryRoutes);
app.use("/auth", loginRoutes);

app.listen(ports, () => console.log(`listening on port ${ports}`));

尝试登录@主页时:

错误:

 POST http://localhost:3000/auth/login net::ERR_CONNECTION_REFUSED ERROR kd {headers: dd, status: 0, statusText: "Unknown Error", url: "http://localhost:3000/auth/login", ok: false, …} error: ProgressEvent {isTrusted: true, lengthComputable: false, loaded: 0, total: 0, type: "error", …} headers: dd {normalizedNames: Map(0), lazyUpdate: null, headers: Map(0)} message: "Http failure response for http://localhost:3000/auth/login: 0 Unknown Error" name: "HttpErrorResponse" ok: false status: 0 statusText: "Unknown Error" url: "http://localhost:3000/auth/login" __proto__: wd

我根本不了解这个反向代理,我想我应该指出我的应用程序路线。 谢谢!

你只需要这么多:

server {
    listen 80;
    listen [::]:80;
    server_name http://MY-AWS-IP.com;
    root /home/ubuntu/dashboard/frontend/dist/frontangular;
    server_tokens off;
    index index.html index.htm;

    location / {
        proxy_pass http://localhost:3000;
        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;
    }   
}

只有当它们将由不同的服务器处理时,您才需要设置不同的位置块,可能在不同的端口上运行。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM