簡體   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