Increase the Nginx request timeout in nginx.conf for Docker
Learn how to configure Nginx request timeouts for Dockerized applications.
Lesson Content
Let’s increase the Nginx request timeout to five minutes. Nginx uses a different configuration process from PHP, so we update the configuration that is mounted into the Nginx container.
The Nginx Docker image is in the images/nginx/1.18 directory. Its Dockerfile copies two configuration files into the container:
nginx.confis copied to/etc/nginx/nginx.conf.default.confis copied to/etc/nginx/conf.d/default.conf.
The main nginx.conf file defines the root-level Nginx configuration. At the bottom, its http directive includes the files in /etc/nginx/conf.d, which loads default.conf.
The default.conf file configures the HTTP-to-HTTPS redirect and SSL certificate. It also includes /var/www/html/nginx.conf:
include /var/www/html/nginx[.]conf;The square brackets around the period tell Nginx to include the file only if it exists. This is important when you first start the project because Magento is not installed yet, so the file may not exist. Without the brackets, Nginx would fail to start when it could not find the file.
Magento provides a recommended Nginx configuration in src/nginx.conf.sample. The Docker Compose development configuration currently mounts that file as nginx.conf inside /var/www/html:
- ./src/nginx.conf.sample:/var/www/html/nginx.conf:cachedSince nginx.conf.sample is a Magento-provided sample file, avoid modifying it directly. Instead, create a project-specific copy and mount that copy.
Open docker-compose.dev.yml and change the bind mount so it uses src/nginx.conf as the source:
- ./src/nginx.conf:/var/www/html/nginx.conf:cachedCopy the sample file to create the new project-specific configuration:
cp src/nginx.conf.sample src/nginx.confThe nginx.conf file is included inside a server directive by default.conf. Add the timeout directives at the root of this file, outside the existing location directives, so they apply to the entire server:
proxy_read_timeout 300;
proxy_connect_timeout 300;
proxy_send_timeout 300;All three values are measured in seconds, so 300 sets each timeout to five minutes.
Restart the Docker services so Nginx loads the updated configuration:
bin/restartOnce the services restart, Nginx allows requests to remain connected for up to five minutes.
Join the discussion!
Comments
Want to comment on this lesson?
Log in to leave comments.