본문 바로가기

# Development/DevOps

[Nginx] 504 Gateway Time-out

504 Gateway Time-out

 

Nginx의 uwsgi time_out 은 디폴트 설정이 60s이다.

불러올 데이터가 많은 특정 API를 부르는 도중에 60초가 초과되어 사이트에서 에러를 뿜었다.

 

임시방편으로 Time-out 시간을 별도로 설정해주었다.

Time-out 시간은 Client와 Nginx의 통신, uWSGI와 Flask 서버의 통신 두 경우 모두 설정해주었다. 

 

Nginx 설정 파일 수정 (nginx.conf)

리눅스 서버에서 sudo 명령어를 사용해서 nginx.conf 파일을 수정 가능한 상태로 열어주고, 

/etc/nginx$ sudo nano nginx.conf

다음과 같이 타임아웃 관련 명령문을 추가해했다.

http{
	proxy_connect_timeout 300;
	proxy_read_timeout 300;
    
	client_body_timeout 300;
	client_header_timeout 300;
}

 

포트별로 timeout을 설정하는 방법은 아래의 공식 문서를 참고하면 된다.

https://nginx.org/en/docs/http/configuring_https_servers.html#optimization

 

Configuring HTTPS servers

Configuring HTTPS servers To configure an HTTPS server, the ssl parameter must be enabled on listening sockets in the server block, and the locations of the server certificate and private key files should be specified: server { listen 443 ssl; server_name

nginx.org

 

uWSGI 설정 파일 수정

그 다음에는 서비스 uwsgi 설정 파일을 열어서 타임아웃 관련 명령문을 추가했다.

/etc/nginx/sites-abailable$ sudo nano myproject.ini
server{
	listen 80;
	server_name myproject;
	location / {
		include uwsgi_params;
		uwsgi_pass 'PASS';
        
		uwsgi_connect_timeout 300;
		uwsgi_read_timeout 300;
		uwsgi_send_timeout 300;
    }
}
 

Nginx timeouts when uWSGI takes long to process request

I have Nginx + uWSGI for Python Django app. I have the following in my nginx.conf: location / { include uwsgi_params; uwsgi_pass 127.0.0.1:9001; uwsgi_read_timeout 1800;

stackoverflow.com

 

 

Nginx 재시작

위와 같이 수정 후 flask 서비스를 재시작했지만 변경사항이 적용이 안되어 계속 에러가 났다.

알고보니 nginx를 수정했으면 flask 서비스를 재시작 하는게 아니라 nginx를 재시작 해야한다.

sudo service nginx reload
sudo service nginx restart
sudo systemctl restart myproject

 

nginx 재시작후 에러 없이 사이트가 정상 운영 되었다.