【问题标题】:Unable to Reidirect in Nginx无法在 Nginx 中重定向
【发布时间】:2021-06-28 09:09:51
【问题描述】:

我向我的服务器添加了一个新网站(“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}`);
})

【问题讨论】:

  • 1.您是否尝试过使用 Ingonito 模式或其他浏览器访问该 url?我遇到了 chrome 的这个问题,我不得不清除缓存让他找到真正的路线而不是缓存的路线
  • @MathieuLescaudron 即使我以隐身方式打开它仍然说无法访问该页面
  • 您的服务器可以使用 localhost 访问吗?
  • @MathieuLescaudron 是的,所有这些都在 1 台服务器下
  • 尝试删除除 isonline 之外的所有 nginx 配置以帮助您调试

标签: node.js reactjs express nginx server


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2017-04-10
    • 2016-04-29
    • 2018-05-18
    • 2016-02-22
    • 2018-12-12
    • 1970-01-01
    • 2017-05-15
    • 1970-01-01
    • 2018-10-25
    相关资源
    最近更新 更多