0

I am learning using nginx to connect server and app in the docker compose. In the app, I am trying to post data to the database and it sends 5 requests a time. The nginx seems not happy with it, and then I got 502 error: POST http://localhost/api/somerequest 502 (Bad Gateway). If I used a lower frequency at 1 request a time and it will work.

The question is whether it possible to improve the nginx performance to allow it to handle multiple large number of requests, e.g. at 5 request frequency. Is there any settings in the configuration I can start with?

The current config file:

user  nginx;
worker_processes  1;

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

include conf.d/events.conf;
include conf.d/http.conf;

events {
    worker_connections 1024;
}

http {
    upstream server {
        server ${SERVER_HOST}:${SERVER_PORT}; # env variable from container
        keepalive 15;
    }

    upstream client {
        server ${CLIENT_HOST}:${CLIENT_PORT}; # env variable from container
        keepalive 15;
    }

    server {
        listen 80;
        server_name myservice; # my service container name in docker-compose.yml

        error_log   /var/log/nginx/myservice.error.log;
        access_log  /var/log/nginx/myservice.access.log;

        location / {
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $remote_addr;
            proxy_set_header Host $http_host;

            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "Upgrade";

            proxy_set_header Connection "Keep-Alive";
            proxy_set_header Proxy-Connection "Keep-Alive";

            proxy_pass http://client;
        }

        location /api {
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $remote_addr;
            proxy_set_header Host $http_host;

            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "Upgrade";

            proxy_set_header Connection "Keep-Alive";
            proxy_set_header Proxy-Connection "Keep-Alive";

            proxy_pass http://server/api;
        }
    }
}

Note: there is one core in the nginx container. I have tried to increase the connection workers to 4000, but it still does not work.

Thank you in advance.

achai
  • 199
  • 1
  • 7
  • 1
    Nginx is generally pretty efficient. If you're getting an HTTP 502 Bad Gateway error back, it's possible that the backend service isn't producing responses quickly enough and the Nginx proxy has decided it's unavailable. – David Maze Jul 16 '21 at 15:23
  • okay, thanks for the information. Assuming the backend is fixed, is it possible to let nginx wait for the response to avoid breaking? – achai Jul 16 '21 at 15:27

1 Answers1

-1

As David said nginx is usually quite efficient. The magic solution of making nginx wait is not really a solution unless you really know what you are doing and the dangers you are exposing yourself to.

I want to give you an example of what you want:

Suppose we make nginx wait for the answer or, failing that, wait for a certain time. Now suppose that for some reason the app crashes for several hours. What will happen in that situation?

UPDATE (Read comments)

location / {
    echo_sleep 1;
    echo_exec @test;
}
location @test {
    echo $echo_request_uri;
}
ICIM
  • 139
  • 1
  • 10
  • Thanks ICIM. Is it possible to just increase a waiting time a little bit? – achai Jul 19 '21 at 15:18
  • I was hoping to be more persuasive that it is not a solution, but I see that you are really interested ... I leave the code in the answer (up) `echo_sleep 1;`. I recommend that you make all requests wait for a few seconds and thus force all requests. (There is no waiting queue) – ICIM Jul 19 '21 at 16:02