首页 > 解决方案 > 无法在 Nginx 中重定向

问题描述

我向我的服务器添加了一个新网站(“thissite.online”),虽然我希望将其重定向到端口“6000”,但 NGINX 不断将其重定向回端口“8000”。如果我关闭端口 8000,它会导致 502(坏网关)。我的代码有什么问题吗?该网站是用 React 和 build 构建的

server {
    server_name admin.abc.tech www.admin.abc.tech;
    location / {
        proxy_pass http://127.0.0.1:8000;
        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;
        proxy_redirect off;
     }
    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/admin.abc.tech/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/admin.abc.tech/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}
server {
    listen 80;
    server_name abc.tech www.abc.tech;
    location / {
        proxy_pass http://127.0.0.1:5000;
        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;
        proxy_redirect off;
     }
}
server {
    listen 80;
    server_name thissite.online www.thissite.online;
    location / {
        proxy_pass http://127.0.0.1:6000;
        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;
        proxy_redirect off;
     }
}
server {
    if ($host = admin.abc.tech) {
        return 301 https://$host$request_uri;
    } # managed by Certbot
    listen 80;
    server_name admin.abc.tech www.admin.abc.tech;
    return 404; # managed by Certbot
}

这是我的服务器代码:这是我通常在启动服务器应用程序时使用的代码

const express = require ('express')

const app  = express()
const PORT  = process.env.PORT || 6000
const path = require('path')

require('dotenv').config()


app.use(express.json({ limit: '50mb' }));
app.use(express.urlencoded({ limit: '50mb', extended: true }));

app.use(require('./Routes/auth'))

app.use(express.static("client/build"));
  
app.get(/^\/(?!api).*/, (req, res) => {
    res.sendFile(path.join(__dirname, "client", "build", "index.html")); // relative path
  });

app.listen(PORT,()=>{
    console.log(`server connected http://localhost:${PORT}`);
})

标签: node.jsreactjsexpressnginxserver

解决方案


代码实际上是正确的,事实证明设置需要时间


推荐阅读