gpt4 book ai didi

node.js - 在运行 Nginx 反向代理的 ubuntu 18.04 AWS 服务器上使用 Nodejs 应用程序的 HTTP 404

转载 作者:行者123 更新时间:2023-12-04 19:10:03 26 4
gpt4 key购买 nike

经过全面测试后,我已将我的 Nodejs (10.16) 应用程序移动到运行 Nginx (17.4) 反向代理的 AWS ubuntu 18.04。我遇到的问题是前端 React Native 请求有 http 404。这是我前端的日志输出:

[12:50:39] I | ReactNativeJS ▶︎ 'response in signup: ', { type: 'default',
│ status: 404, //<<<=== Server Not Found
│ ok: false,
│ statusText: undefined,
│ headers:
│ { map:
│ { connection: 'keep-alive',
│ 'content-length': '153',
│ 'content-type': 'text/html',
│ date: 'Wed, 09 Oct 2019 19:50:40 GMT',
│ server: 'nginx/1.17.4' } }, //<<<=== Nginx
│ url: 'http://my-aws-public-ip/api/users/signup', //<<<=== request URL

这是在 ubuntu 服务器上使用 node index.js 启动应用程序后的日志输出:
Listening on port 3000...
Executing (default): SELECT 1+1 AS result
DB connection has been established successfully.

此处服务器正在监听端口 3000并且数据库连接已成功建立。

这里是 nginx 代理配置文件 default/etc/nginx/sites-available/ 下:
$cat /etc/nginx/sites-available/default
server {
listen 80;
server_name localhost;

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

这里是 db.js在 nodejs 应用程序中:
const Sql = require("sequelize");
const db = new Sql('emps', 'postgres', `${process.env.DB_PASSWORD}`, {
host: 'localhost',
dialect: 'postgres',
port:5432,
} );

db
.authenticate()
.then(() => {
console.log('DB connection has been established successfully.');

})
.catch(err => {
console.error('Unable to connect to the database:', err);
});

module.exports = db;

这是 index.js 的结尾:
server.listen(port, () => {  //port=3000
//logger('info', `Listening on port ${port}...`);
console.log(`env var: ${process.env.jwtPrivateKey}`)
console.log(`Listening on port ${port}...`);

});

这里是 routes.js :
module.exports = function(app, io) {
app.use(helmet());
app.use(compression());
app.use(express.json());
app.use('/api/events', events);
app.use('/api/messages', messages);
app.use('/api/users', users);
app.use('/api/groups', groups);
app.use('/api/contacts', contacts);
app.use('/api/groupmembers', groupmembers);
app.use('/api/sms', smsverif);
app.use(error);
}

在 AWS 安全组上,入站 TCP port 80, 22, 443 and 3000对所有交通开放。导致 http 404 的配置中缺少什么?

最佳答案

试试这个 nginx 的配置

server {
listen 80;
server_name localhost your-aws-public-ip default_server;

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

关于node.js - 在运行 Nginx 反向代理的 ubuntu 18.04 AWS 服务器上使用 Nodejs 应用程序的 HTTP 404,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58311816/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com