提问人:Willis0 提问时间:11/17/2023 更新时间:11/17/2023 访问量:28
在 AWS EC2 实例上更新前端后,尝试通过 Nginx 与 Node.js 后端通信时,为什么会收到 502 错误?
Why am I receiving a 502 error when attempting to communicate with my Node.js backend through Nginx after updating my frontend on an AWS EC2 instance?
问:
我在 AWS EC2 实例上运行 nginx Web 服务器 在同一实例上,我正在运行前端 Web 应用程序 (reactjs)(端口 3000)和后端 nodejs 服务器(端口 3002) 在我在前端进行更新之前,一切都正常工作,现在我无法向后端发送请求(无论是从前端的 api 请求还是使用 curl)。当我提出请求时,我收到 502 错误。 我已经检查了没有防火墙禁止访问我的后端 后端正在运行,但当我向它发送一个请求时,它没有记录任何请求 你能帮我弄清楚为什么我无法向后端发送请求,而是收到 502 错误吗?
NGINX的:
server {
server_name abc.co.uk;
listen 80;
return 301 https://$host$request_uri;
}
server {
server_name abc.co.uk;
location / {
proxy_pass http://localhost:3000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_read_timeout 300s;
proxy_connect_timeout 75s;
}
location /api {
proxy_pass http://localhost:3002;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_read_timeout 300s;
proxy_connect_timeout 75s;
}
location /.well-known/acme-challenge/ {
alias /usr/share/nginx/html/.well-known/acme-challenge/;
}
listen [::]:443 ssl ipv6only=on;
listen 443 ssl;
ssl_certificate /etc/letsencrypt/live/abc.co.uk/full_chain.pem;
ssl_certificate_key /etc/letsencrypt/live/abc.co.uk/private_key.pem;
include /etc/letsencrypt/options-ssl-nginx.conf;
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
add_header Strict-Transport-Security "max-age=31536000" always;
}
后端:
const express = require('express');
const https = require('https');
const fs = require('fs');
const cors = require('cors');
const morgan = require('morgan');
const path = require('path');
const app = express();
const PORT = 3002;
const accessLogStream = fs.createWriteStream(path.join('./', 'access.log'), { flags: 'a' });
app.use(morgan('combined', { stream: accessLogStream }));
// Retrieve SSL certificate and key file paths
const readCertificates = async () => {
try {
const sslPrivKeyPath = './private_key_path;
const sslFullChainPath = './full_chain_path;
const key = fs.readFileSync(sslPrivKeyPath, 'utf8');
const cert = fs.readFileSync(sslFullChainPath, 'utf8');
return { key, cert };
} catch (error) {
console.error('Error reading SSL certificates:', error);
throw error;
}
};
app.use(express.json());
app.use(cors());
const logRequests = (req, res, next) => {
console.log(`Incoming request: ${req.method} ${req.originalUrl}`);
next();
};
const logErrors = (err, req, res, next) => {
console.error('Error encountered:', err);
next(err);
};
const errorHandler = (err, req, res, next) => {
if (!res.headersSent) {
res.status(500).send('Internal server error');
}
};
app.use(logErrors);
app.use(errorHandler);
app.use(logRequests);
app.get('/exampleapi', async (req, res) => {
try {
const apiKey = 123
res.json({
apiKey
});
} catch (err) {
console.error('Error fetching parameters:', err);
res.status(500).send('Internal server error');
}
});
const startServer = async () => {
try {
const httpsOptions = await readCertificates();
const httpsServer = https.createServer(httpsOptions, app);
httpsServer.listen(PORT, '0.0.0.0', () => {
console.log(`Server running on port ${PORT}`);
});
} catch (error) {
console.error('Error starting the server:', error);
}
};
startServer();
前端:
import axios from 'axios';
export const getRequest = async () => {
try {
await fetchPublicIPv4();
const response = await axios.get(`https://abc.co.uk/api/exampleapi`);
return response.data;
} catch (error) {
console.error('Error fetching config:', error);
throw error;
}
};
当我发出请求时,我在 NGINX 日志中收到此错误:
信号处理已开始 从 123 接收到信号 1 (SIGHUP),重新配置 重新配置 使用“epoll”事件方法 启动工作进程 启动工作进程 123 正常关闭 退出 退出 从 2037217 接收到的信号 17 (SIGCHLD) 工作进程 123 退出,代码为 0 接收到信号 29 (SIGIO) *14 从上游读取响应头时,上游过早关闭连接,客户端:12.34.56.78,服务器:abc.co.uk,请求:“GET /api/exampleapi HTTP/1.1”,上游:“http://127.0.0.1:3002/api/exampleapi”,主机:“abc.co.uk”,引用者:“https://abc.co.uk/”
我尝试重新启动所有单独的服务器,将所有代码重新上传到我的 EC2 实例 我为代理路由添加了超时限制
答: 暂无答案
评论