Ícaro Bichir bio photo

Ícaro Bichir

Geek that dreams to be a hacker, became a intern security, a DevOps Analyst and now, SysAdmin with DevOps Engineering thinking. ;)

Email LinkedIn Github Last.fm

General Vision

I’ve assumed that you already have nginx installed on your system, if not don't worry, you can access the nginx official repository and install it.

How it works

Nginx is a amazing web server and reverse proxy. It can be configured in different million ways and great things can be done. It’s often used side by side with another server’s, like Guicorn or Tomcat. Nginx allows buffered and queued clients, generating more speed to the application worker process.

This is the default configuration for nginx reverse proxy, using proxy_pass to redirect the requests on port 8080:

server {
    proxy_pass http://localhost:8080
}

This simple and reliable configuration works on almost every environment, servers and applications. It’s a 1-1 connection between the nginx web server and the application server. When you identify any error on this, probably there is some problem with your backend. I'm sorry about that, fella

Life ain’t all sunshine and rainbows

When we use AWS ELB, it works on 90% of the time, because elastic load balancers have the habit to change their IP address and sometimes, when this occurs, run to the hills the request continuous to resolve the “old” IP address, on the default configuration of upstream and proxy modules the nginx never resolve DNS on runtime, just at startup, causing the service failure, because the backend continues sending the requests to the dropped IP.

Configure to “re-resolve” DNS

There is a way to force nginx to re-resolve DNS during the application uptime Thankfully, using resolver, proxy_pass, upstream feature and regular expressions you can force nginx to check if the DNS is working. If not, it loads the new IP.

resolver 10.10.10.2;

server {
  set $upstream_endpoint your_elb_address_here.us-east-1.elb.amazonaws.com;
  
  location /api {
    rewrite ˆ/api(.*) /$1 break;
    proxy_pass http://$upstream_endpoint/api;
  }

  location /website {
    rewrite ˆ/website(.*) /$1 break;
    proxy_pass http://$upstream_endpoint/website;
  }
}

Nginx >= 1.1.9 will re-resolve DNS records based on their TTL, but with this little configuration, your nginx web server will be able to override the DNS records.

“End Of Line” – The MCP, TRON

If you have some question or update about this procedure, please contact me.