Setup Nginx for MSA Development

Problems to solve

Our product consists of several sub-systems. Each sub-system is developed and maintained by a different team. And code-wise, the entire product is organized with Maven in a big Git repository (acutally, better to be splitted) and each sub-system is maintained as a Maven module.

Originally, to setup environment for development, we have to build all modules besides the ones we are turely responsible for. The drawback is obvious, it takes two much time to build the whole project. And as the number of modules increases, this problem becomes worser. In addition, to incooperate changes from other module during development, each time we have to recompile those modules. Although we can utilize Maven to only build partial or specific modules, it’s still a bit annoying.
Last but not least, it can be quite difficult to setup everything you needed if one modules have many indirect dependencies, which may require different configurations to startup.

Solution

To migarte this problem, we maintain a running product instance containing all sub-systems, which is updated on a regular basis (Say one day). Let’s call such environment as “Development environment”. And setup Nginx locally to route the requests generated during development to the right place. Specifically, say I’m developing module A, then I will instruct the Nginx instance to route all requests to module A to my local web server, while route all other requests to “Development enviroment”.

In this way, we only need to care about the module we developed. Other modules are built and updated automatically.

Setup Nginx for routing

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
server {
# make your nginx listen to 8089
listen 8089 default_server;

root /var/www/html;

index index.html index.htm;

server_name _;

location / {
proxy_set_header Host $http_host;
# suppose your local server listens to 8080
proxy_pass http://localhost:8080;
# enable intercepting HTTP errors codes
proxy_intercept_errors on;
# internal rediret to error page defined by "@404found" when the error code is 404, and override the return code
error_page 404 = @404found;
# the following config is needed for websocket proxying
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
}

location @404found {
proxy_set_header Host $http_host;
proxy_pass <URL for your development enviroment>;
# the following config is needed for websocket proxying
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
}
}

The above Nginx config, you need to access your local development server via localhost:8089 and the Nginx server will do the routing for you.

Debug Nginx Setting

It would be greate to debug your Nginx setting. It can be annoying when you make some wrong configuration but don’t know where it is. Refer to this URL for debugging