nginx 將一些 POST 請求掛起一段時間

nginx 將一些 POST 請求掛起一段時間

我在 Ubuntu 18.08 上運行以下堆疊並定義為 docker-compose:

  1. 的實例mariadb:10.3.20
  2. 基於其上wordpress:5.3.0-php7.2安裝的自訂 WordPress 實例ioncube
  3. nginx:1.13基於安裝的nginx-amplify-agent自訂 nginx 實例

nginx的設定:

user  nginx;
worker_processes  auto;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;

events {
    worker_connections  10000;
}

http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                          '$status $body_bytes_sent "$http_referer" '
                          '"$http_user_agent" "$http_x_forwarded_for"';

    log_format  main_ext  '$remote_addr - $remote_user [$time_local] "$request" '
                          '$status $body_bytes_sent "$http_referer" '
                          '"$http_user_agent" "$http_x_forwarded_for" '
                          '"$host" sn="$server_name" '
                          'rt=$request_time '
                          'ua="$upstream_addr" us="$upstream_status" '
                          'ut="$upstream_response_time" ul="$upstream_response_length" '
                          'cs=$upstream_cache_status' ;

    access_log  /var/log/nginx/access.log  main_ext;
    error_log  /var/log/nginx/error.log warn;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    gzip  on;

    include /etc/nginx/conf.d/*.conf;
}

網站的定義方式如下:

server {
        listen 80;
        listen [::]:80;

        server_name some.org www.some.org;

        location / {
                rewrite ^ https://$host$request_uri? permanent;
        }
}

server {
        listen 443 ssl http2;
        listen [::]:443 ssl http2;
        server_name some.org www.some.org;

        index index.php index.html index.htm;

        root /var/www/html;

        server_tokens off;

        ssl_certificate /etc/letsencrypt/live/some.org/fullchain.pem;
        ssl_certificate_key /etc/letsencrypt/live/some.org/privkey.pem;

        add_header X-Frame-Options "SAMEORIGIN" always;
        add_header X-XSS-Protection "1; mode=block" always;
        add_header X-Content-Type-Options "nosniff" always;
        add_header Referrer-Policy "no-referrer-when-downgrade" always;
        add_header Content-Security-Policy "default-src * data: 'unsafe-eval' 'unsafe-inline'" always;
        # add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload" always;
        # enable strict transport security only if you understand the implications

        location / {
                proxy_connect_timeout 600;
                proxy_send_timeout    600;
                proxy_read_timeout    600;
                proxy_redirect        off;
                proxy_pass http://wordpress;

                proxy_set_header      X-Real-IP $remote_addr;
                proxy_set_header      X-Forwarded-Proto https;
                proxy_set_header      X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header      Host $host;
        }

        location = /favicon.ico {
                log_not_found off; access_log off;
        }

        location = /robots.txt {
                log_not_found off; access_log off; allow all;
        }

        location ~* \.(css|js|gif|ico|jpeg|jpg|png)$ {
                expires max;
                log_not_found off;
        }
}

整個堆疊按預期工作,除非有10-15 個使用者造訪網站並嘗試執行操作。後被掛起。在掛起期間,同一瀏覽器不再對網站負責(但從其他瀏覽器來看一切都很好!)。一旦請求發布 - 網站將再次負責。

日誌也很奇怪:

  • 一旦掛起/掛起的請求到達伺服器,它就會顯示在 nginx 存取日誌中,但不會顯示在 WordPress 日誌中
  • 一旦請求最終被釋放(即處理),它會在 nginx 存取日誌和 wordpress 日誌中第二次顯示,但時間不同

nginx訪問日誌:

62.96.39.243 - - [17/Jan/2020:10:56:41 +0000] "GET /something

78.43.40.52 - - [17/Jan/2020:10:56:41 +0000] "POST /ajax-bidsform.html?meth=post&yid=d7f9f1a1a0bf HTTP/2.0" 200 208 "https://some.org/xchange_XRP_to_SBERRUB/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.117 Safari/537.36" "-"

78.43.40.52 - - [17/Jan/2020:10:56:41 +0000] "GET /something

WordPress:

134.19.130.91 - - [17/Jan/2020:10:56:40 +0000] "GET /something

78.43.40.52 - - [17/Jan/2020:10:50:07 +0000] "POST /ajax-bidsform.html?meth=post&yid=d7f9f1a1a0bf HTTP/1.0" 200 559 "https://some.org/xchange_XRP_to_SBERRUB/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.117 Safari/537.36"

62.96.39.243 - - [17/Jan/2020:10:56:41 +0000] "GET /something

如您所看到的,POST /ajax-bidsform.html...nginx 日誌中的掛起時間為 10:56,但 WordPress 中的掛起時間為 10:50 - 這正是用戶端完成此要求的時間。據我了解,這意味著請求在 nginx 層級的某個地方停留了近 6 分鐘,直到它實際傳遞到 wordpress。正如你所看到的,我沒有 nginx 的 ddos​​ 保護指令。

我這邊還有一些註釋:在掛起請求期間,沒有任何 CPU 或 RAM 長期峰值,因此它可能與硬體問題無關。我還認為它在某種程度上與掛起腳本(即ajax-bidsform.html)有關,但只有當我們從虛擬主機遷移到digital-ocean 的雲端實例時(以前從未發生過),它才開始發生,所以我想這是一個配置問題。日誌中請求的時間軸也可以證明這一點。

到目前為止我確實嘗試過:

  1. 增加worker_connections至10000
  2. 將 nginx 執行個體(不是主機的執行個體)增加到net.core.somaxconn1024

但問題仍然發生..任何想法或想法將不勝感激!

答案1

nginx 日誌中為 10:56,但 WordPress 日誌中為 10:50

我將其解釋為 wordpress 在 10:50 接收請求並在 10:56 將結果傳回給 nginx。為了確定,你可以在 nginx.conf 中upstream_response_time加入:log_format使用 NGINX 日誌記錄進行應用程式效能監控

相關內容