隨后的 HTTP 請求 GET 和 POST 有時需要 60 秒才能得到回應,而其他時候請求會在 100 毫秒內得到回應。我應該如何嘗試解決這個問題?我認為這與我的 nodejs 后端和 mysql 連接有關,不一定與它們都存在的 nginx 反向代理和 docker 容器有關。
這是我經常遇到的 nginx 錯誤:
2022/01/10 16:43:15 [error] 33#33: *985 upstream timed out (110: Connection timed out) while connecting to upstream, client: xxx.xxx.xxx.xxx, server: sub.domain.tld, request: "GET /getTemp?api_key=xxxxxxx HTTP/1.1", upstream: "http://127.0.0.1:8080/getTemp?api_key=xxxxxxx", host: "sub.domain.tld"
我已嘗試proxy_read_timeout按照其他帖子的建議將 nginx 增加到 120 秒,但這不是這里的解決方法。我也從mysql.createConnection()to切換,mysql.createPool()但這并沒有解決它。
這是我到 mysql 資料庫的 nodejs 連接:
let pool = mysql.createPool({
connectionLimit: 10,
host: process.env.DB_HOST,
user: process.env.DB_USERNAME,
password: process.env.DB_PASSWORD,
database: process.env.DB_DATABASE,
timezone: 'Europe/Helsinki'
});
這是 getTemp 端點的片段:
app.get('/getTemp', (req, res) =>{
console.log(req.path);
let ip = req.header('x-forwarded-for') || req.socket.remoteAddress;
let ua = req.get('user-agent');
console.log(ua);
let {api_key} = req.query;
console.log(req.query);
if(api_key == process.env.API_KEY){
console.log(`Authorized access from IP: '${ip}'`);
// if one of the variables are undefined then send 400 status code to the client
if(api_key == undefined){
return res.sendStatus(400).send({message:"One or more variables are undefined"});
}
sql_query = "SELECT * FROM tempData ORDER BY id DESC";
sql_query = mysql.format(sql_query);
// attempt to query mysql server with the sql_query
pool.query(sql_query, (error, result) =>{
if (error){
// on error log the error to console and send 500 status code to client
console.log(error);
return res.sendStatus(500);
};
// if temp is found we send 200 status code to client
if(result.length > 0){
let currentTemp = result[0].currentTemp;
console.log(`Temp: ${currentTemp}`);
return res.status(200).send({temperature:currentTemp});
}else{
// incase temp is not found then we send 500 status code to client
console.log('Temperature not found');
return res.status(500).send({error:"Temperature not found"});
}
});
}else{
// if client sends invalid api key then send 403 status code to the client
console.log(`Unauthorized access using API key '${api_key}' from IP: '${ip}'`);
return res.sendStatus(403);
}
})
這是我的 nginx 配置:
user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;
events {
worker_connections 4096;
}
http {
server {
listen 80;
listen [::]:80;
listen 443 default_server ssl;
listen [::]:443 ssl;
ssl_certificate /cert.pem;
ssl_certificate_key /cert.key;
server_name sub.domain.tld;
location / {
proxy_read_timeout 120;
proxy_pass http://localhost:8080;
proxy_set_header X-Real-IP $http_cf_connecting_ip;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
}
}
}
uj5u.com熱心網友回復:
我為端點分配了靜態值,因此它不會從資料庫中查詢任何內容,并且回應也需要一分鐘,因此不是資料庫連接或導致問題的端點。我在應用程式中禁用了速率限制,并且不再超時。所以我超時的原因是配置錯誤的速率限制。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/410157.html
標籤:
