node.js - 无法在 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}`);
})
解决方案
代码实际上是正确的,事实证明设置需要时间
推荐阅读
- sql - Getting SUM of 'x' consecutive values where x is an integer from another row
- c# - 在 Xamarin Android 的 ArrayAdapter 中更新项目的值
- asp.net-mvc-4 - 如何在 MVC 中同时使用 Ajax.BeginForm 和 Html.BeginForm 解决空模型问题
- c++ - 宏与内联
- node.js - 使用sequelize在postgres中存储字符串数组?
- mysql - 使用分区获取表中每个项目的最新记录的 SQL 替代方法
- python - 获取 3d numpy 的最大值
- angular9 - 如何解决 node_module/webpack-dev-middleware/lib/util.js/131 中的范围问题
- python - 如何将此 for 循环转换为 Python 列表理解?
- cuda - CUDA 浮点精度与 CPU 实现不匹配