'Nginx response spends too long time after running several day, how to deal with it?

I'm using Nginx in Kubernetes as a reverse server. The question is after running several days the nginx server gets wrong, almost every three or four requests from client have a very long response time, the normal request spends about 200ms~500ms, and the unnormal reqeust even spends more than 1min! But when I restart nginx server at this time the reqeusts are all normal, and after running several days it will reappear that I describe above.

Here is the nginx deployment I use:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: external-api-nginx
spec:
  replicas: 1
  selector:
    matchLabels:
      app: external-api-nginx
  template:
    metadata:
      labels:
        app: external-api-nginx
      annotations:
        sidecar.istio.io/inject: "false"
    spec:
      containers:
        - name: external-api-nginx
          image: nginx:1.21.4
          ports:
          - containerPort: 80
            protocol: TCP
          volumeMounts:
          - name: external-api-nginx-conf
            mountPath: /etc/nginx/nginx.conf
            subPath: nginx.conf
      volumes:
        - name: external-api-nginx-conf
          configMap:
            name: external-api-nginx-conf
            items:
            - key: nginx.conf
              path: nginx.conf
---
apiVersion: v1
kind: Service
metadata:
  name: external-api-nginx
  labels:
    app: external-api-nginx
spec:
  ports:
    - port: 80
      targetPort: 80
      protocol: TCP
      name: http
  selector:
    app: external-api-nginx

---
apiVersion: v1
kind: ConfigMap
metadata:
    name: external-api-nginx-conf
data:
  nginx.conf: |
    user  nginx;
    worker_processes  1;

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


    events {
        worker_connections  1024;
    }


    http {
        include       /etc/nginx/mime.types;

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

        access_log  /var/log/nginx/access.log  main;

        # sendfile        on;
        #tcp_nopush     on;

        keepalive_timeout  65;

        #gzip  on;
        client_max_body_size   10m;
        client_body_buffer_size 1024k;

        server {
          listen       80;
          server_name  tip.mesoor.com;

          location ~* "^/cvparser/(.*)" {
              rewrite "(?i)/cvparser/(.*)" /$1 break;
              proxy_pass http://cvparser.mesoor-api.com;
              proxy_http_version 1.1;
          }
          location ~* "^/knowledge/(.*)" {
              rewrite "(?i)/knowledge/(.*)" /$1 break;
              proxy_pass http://data-engine.mesoor-api.com;
              proxy_http_version 1.1;
          }
          location ~* "^/intentRecognize/(.*)" {
              rewrite "(?i)/intentRecognize/(.*)" /$1 break;
              proxy_pass http://search-intent-recognizer.mesoor-api.com;
              proxy_http_version 1.1;
          }
          location ~* "^/jobWriter/(.*)" {
              rewrite "(?i)/jobWriter/(.*)" /$1 break;
              proxy_pass http://job-writer.mesoor-api.com;
              proxy_http_version 1.1;
          }
          location ~* "^/parseJob/(.*)" {
              rewrite "(?i)/parseJob/(.*)" /$1 break;
              proxy_pass http://job-parser.mesoor-api.com;
              proxy_http_version 1.1;
          }
          location ~* "^/persona/(.*)" {
              rewrite "(?i)/persona/(.*)" /$1 break;
              proxy_pass http://persona.mesoor-api.com;
              proxy_http_version 1.1;
          }
          location ~* "^/rechend/(.*)" {
              rewrite "(?i)/rechend/(.*)" /$1 break;
              proxy_pass http://rechend.mesoor-api.com;
              proxy_http_version 1.1;
          }
          location ~* "^/space/(.*)" {
              rewrite "(?i)/space/(.*)" /$1 break;
              proxy_pass http://space.mesoor-api.com;
              proxy_http_version 1.1;
          }
          location ~* "^/matching/(.*)" {
              rewrite "(?i)/matching/(.*)" /$1 break;
              proxy_pass http://matching.mesoor-api.com;
              proxy_http_version 1.1;
          }
          location ~* "^/air/(.*)" {
              rewrite "(?i)/air/(.*)" /$1 break;
              proxy_pass http://air.mesoor-api.com;
              proxy_http_version 1.1;
          }
          location ~* "^/data-sync/(.*)" {
              rewrite "(?i)/data-sync/(.*)" /$1 break;
              proxy_pass http://data-sync.mesoor-api.com;
              proxy_http_version 1.1;
          }
          location ~* "^/dedupe/(.*)" {
              rewrite "(?i)/dedupe/(.*)" /$1 break;
              proxy_pass http://dedupe-ng-server;
              proxy_http_version 1.1;
          }
        }

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

As I'm new to nginx so the nginx.conf is almost default, so I guess maybe some configurations I missed, can some body give me some tips to resolve the problem? Thanks so mush.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source