從一週前開始,Nginx 下載小檔案和提供內容的速度變得非常慢。例如,在同一台伺服器上下載 500kb 檔案的時間從毫秒縮短到 6 秒。感覺就像被某種方式節流了。奇怪的是,我在過去的幾個月裡沒有更改 Nginx 的任何設定檔。
所以我檢查了我的伺服器是否有問題。我透過 sftp 上傳和下載文件,在 1GB 連線上達到 900mb/s。我還檢查了 cpu 和磁碟使用情況,但似乎一切正常。
我用 Postman 做了一些測試,發現實際的 HTTP 回應本身的時間是一些毫秒,但下載檔案之後需要很長時間。
當我使用 HTTP 而不是 HTTPS 時,會發生這種情況,這也很奇怪。 HTTPS 在本機網路上運作完全正常,但如果我從網路外部透過 HTTPS 下載相同的文件,速度又會變慢。 HTTP 總是很慢。
我不知道現在該怎麼辦,或者我還可以檢查什麼。我還創建了一個具有相同配置的新 Ubuntu 虛擬機,我也遇到了相同的問題,所以我猜它與配置有關,但我不知道是什麼。
這是我的 nginx.conf 文件
user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;
events {
worker_connections 768;
# multi_accept on;
}
http {
##
# Basic Settings
##
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
# server_tokens off;
# server_names_hash_bucket_size 64;
# server_name_in_redirect off;
include /etc/nginx/mime.types;
default_type application/octet-stream;
##
# SSL Settings
##
ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
ssl_prefer_server_ciphers on;
##
# Logging Settings
##
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
##
# Gzip Settings
##
gzip on;
gzip_vary on;
gzip_proxied any;
gzip_comp_level 6;
gzip_buffers 16 8k;
gzip_http_version 1.1;
gzip_min_length 256;
#gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript application/vnd.ms-fontobject application/x-font-ttf font/opentype image/svg+xml image/x-icon;
##
# Virtual Host Configs
##
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
### Include a blocklist file
include /etc/nginx/nginx-acces.conf;
}
#mail {
# # See sample authentication script at:
# # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
#
# # auth_http localhost/auth.php;
# # pop3_capabilities "TOP" "USER";
# # imap_capabilities "IMAP4rev1" "UIDPLUS";
#
# server {
# listen localhost:110;
# protocol pop3;
# proxy on;
# }
#
# server {
# listen localhost:143;
# protocol imap;
# proxy on;
# }
#}
這是我啟用網站的配置之一:
server {
server_name resources.example.io www.resources.example.io;
location / {
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://192.168.1.105:8083;
allow all;
}
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/resources.example.io/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/resources.example.io/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 {
if ($host = resources.example.io) {
return 301 https://$host$request_uri;
} # managed by Certbot
server_name resources.example.io www.resources.example.io;
listen 80;
return 404; # managed by Certbot
}