I am working on deploying nginx + php-fpm web server to AWS CloudFront.
My origin web server works perfect by itself.
But when accessed via CloudFront, about 10% of attempts fail with 504 gateway timeout error.
All the php files are served with response header Content-Control: no-store, no-cache, must-revalidate
from origin server so that CloudFront does not cache responses for those php files.
Origin server serves only http and also CloudFront origin server setting was done accordingly.
I was watching server's network traffic with tcptack to see what's happening. When it works fine, I could see those traffic that CloudFront request for php files. But whenever CloudFront produces 504 error, nothing from CloudFront hit the origin server.
Its really painful cause symptom is not consistent. Mostly, it works fine but sometime it doesn't. Spent hours to fix this but still have no clue. Any suggestion and guidance will be appreciated.
First thought was CloudFront might caching those php files response. I checked it with browser DevTool many times, and I could see CloudFront request to origin server correctly. So I thought my server might have connection issue. So I checked connection with curl on several PCs that I can use and no problem. And tried to connect web server with VPN had no problem neither. And I tried my origin server to serve both http and https and changed CloudFront setting to it but had no difference.
[Edit] Parfait suggested to add detailed information.
Here's my nginx server block
server {
listen 8082;
server_name *.domain.origin;
root /home/origin/www;
index index.html index.php;
client_header_timeout 60;
client_body_timeout 60;
client_max_body_size 256M;
keepalive_timeout 30;
proxy_connect_timeout 120;
proxy_send_timeout 120;
proxy_read_timeout 120;
send_timeout 120;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/web_origin.sock;
fastcgi_index index.php;
include fastcgi.conf;
fastcgi_connect_timeout 120;
fastcgi_send_timeout 120;
fastcgi_read_timeout 120;
}
}
And my CloudFront origin setting
And behavior setting.