Using Nginx to reverse proxy for microservices.

It is a pretty common practice for ISP’s to give you a /29 (or charge you). If you have a bunch of microservices running you’ll quickly outlive the usefulness of the /29 and need a way to offer up those sweet, sweet services to people on the outside of your network.

I covered setting up nginx with geoip2 module and associated geoip database in an earlier blog post. Follow those if you need a working example to get rolling.

Conceptually the following is what we are shooting for.

The first step is to make a few directories.

mkdir /usr/local/nginx/conf/sites-available
mkdir /usr/local/nginx/conf/sites-enabled

Then to create a domain file in the sites-available directory.
vi /usr/local/nginx/conf/sites-available/webmail

server {
    listen 80;
    server_name webmail.domain.com;
    location / {
        proxy_pass http://192.168.0.25;
    }
}

Then link make this site available by soft linking it from the sites-available folder.

cd /usr/local/nginx/conf/sites-enabled
ln -s ../sites-available/webmail

If you have followed the previous tutorials so far you’ll need to make sure that sites-enabled is being used by the nginx server by editing the main config. Include the “include” line like below in the http section:
vi /usr/local/nginx/conf/nginx.conf

http {
   access_log  /var/log/nginxaccess.log;
   include /usr/local/nginx/conf/sites-enabled/*;
   geoip2 /usr/share/GeoIP/GeoLite2-Country.mmdb {
       auto_reload 60m;
       $geoip2_metadata_country_build metadata build_epoch;
       $geoip2_data_country_code country iso_code;
       $geoip2_data_country_name country names en;
    }
   geoip2 /usr/share/GeoIP/GeoLite2-City.mmdb {
       auto_reload 60m;
       $geoip2_metadata_city_build metadata build_epoch;
       $geoip2_data_city_name city names en;
    }
   fastcgi_param COUNTRY_CODE $geoip2_data_country_code;
   fastcgi_param COUNTRY_NAME $geoip2_data_country_name;
   fastcgi_param CITY_NAME    $geoip2_data_city_name;

}

That is about it. Just continue to create the file in sites-available then soft link them in sites-enabled and reload nginx.

Leave a comment

Your email address will not be published. Required fields are marked *