![](/img/trans.png)
[英]Nginx, and mysql timeout - upstream timed out (110: Connection timed out)
[英]NGINX and nodejs upstream connection timed out while connecting to upstream but request will get responded after exactly 60s
隨后的 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"
我已嘗試按照其他帖子的建議將 nginx proxy_read_timeout
增加到 120 秒,但這不是這里的解決方法。 我也從mysql.createConnection()
切換到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;
}
}
}
編輯:解決了 nodejs 中配置錯誤的 rateLimit。
我將 static 值分配給端點,因此它不會從數據庫中查詢任何內容,並且響應也需要一分鍾,因此不是數據庫連接或端點導致了問題。 我在應用程序中禁用了 rateLimiting,並且不再超時。 所以我超時的原因是配置錯誤的reteLimit。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.