This short post specifically targets:
– RoR as web development environment – Can be ignored as this is closely related to nginx configuration settings
– Nginx as web server
While working with ElasticBeanstalk environments, one of my requirement was to send a heavy job to Worker App that takes > 1 min (60 seconds) to complete and I had no configuration in place. The way beanstalk-app was handling that job was disappointing. That job never finishes within a minute and another job (duplicate) gets triggered by the app and this goes on till it reaches the max retry count.
I tried googling the solution and found that we have to increase the timeout
for the web server which is handling the request. In my case it was `nginx
`.
So I decided to write a config file under `.ebextensions/02_nginx_proxy.config
` — I am not going into the details of `ebextensions
` here as this is out of scope of this article.
Here’s how my config file looks like:
files: | |
/etc/nginx/conf.d/proxy.conf: | |
mode: "000755" | |
owner: root | |
group: root | |
content: | | |
client_max_body_size 20M; | |
proxy_send_timeout 600; | |
proxy_read_timeout 1h; | |
send_timeout 600; | |
server_names_hash_bucket_size 128; | |
upstream backend { | |
server unix:///var/run/puma/my_app.sock; | |
} | |
map $uri $preferred_proto { | |
default "https"; | |
~^/(assets|public|packs)/ "none"; | |
~^/health_check "http"; | |
} | |
server { | |
listen 80; | |
if ($preferred_proto = "http") { | |
set $http_x_forwarded_proto $preferred_proto; | |
} | |
if ($preferred_proto = "none") { | |
set $preferred_proto $http_x_forwarded_proto; | |
} | |
if ($preferred_proto != $http_x_forwarded_proto) { | |
rewrite ^(.*) $preferred_proto://$host$request_uri redirect; | |
} | |
location / { | |
proxy_pass http://backend; | |
proxy_set_header Connection ""; | |
proxy_http_version 1.1; | |
proxy_set_header Host $http_host; | |
proxy_set_header X-Real-IP $remote_addr; | |
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; | |
proxy_set_header X-Forwarded-Proto https; | |
proxy_set_header X-NginX-Proxy true; | |
location /packs { | |
root /var/app/current/public; | |
gzip_static on; | |
gzip on; | |
expires max; | |
add_header Cache-Control public; | |
} | |
} | |
location /assets { | |
alias /var/app/current/public/assets; | |
gzip_static on; | |
gzip on; | |
expires max; | |
add_header Cache-Control public; | |
} | |
location /public { | |
alias /var/app/current/public; | |
gzip_static on; | |
gzip on; | |
expires max; | |
add_header Cache-Control public; | |
} | |
} |
Here if you focus on lines:
`proxy_send_timeout 600;
`
`proxy_read_timeout 1h;
`
`send_timeout 600;
`
This will prevent sending multiple requests of same job type until the current one finishes in 1h.
If you need any inputs on what other lines are all about/doing, then leave your comment.
Happy Coding!