Category Archives: Tips and Tricks

Using NGINX for Proxy Pass

Lets say I have two servers running on my laptop
1. http://localhost:3000 – This is the UI
2. http://localhost:4000 – This is the web services API

So now I need to setup something like this:
1. http://localhost/ – This should point to UI
2. http://localhost/api/ – This should point to web services API

To set this up I have used NGINX and added this section in the “server” section of the nginx.conf file. On my Mac system this file is located at: /usr/local/etc/nginx/nginx.conf

location /api/ {
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://localhost:4000/;
}

location / {
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://localhost:3000/;
}

To reload NGINX I use the “-s reload” parameter for nginx. On my Mac I execute this command:
/usr/local/Cellar/nginx/1.10.0/bin/nginx -s reload

User input in shell script

I needed to write a small shell script which would allow me to make some decision based on user choices. I needed the user to specify Yes, No or Cancel for an operation. The following piece of hack does the job well.

#! /bin/bash

# define constants
declare -r TRUE=0
declare -r FALSE=1

user_choice() {
local str="$@"
while true
do
# Prompt user, and read command line argument
read -p "$str " answer

# Handle the input we were given
case $answer in
[yY]* ) return $TRUE;;

[nN]* ) return $FALSE;;

[cC]* ) exit;;

* ) echo "Y - Yes, N - No, C - Cancel. Please choose valid option.";;
esac
done
}

if user_choice "Execute Job 1? "; then
echo "Executing Job 1.."
fi
if user_choice "Execute Job 2? "; then
echo "Executing Job 2.."
fi

The following links were referred to achieve this little script:
http://linuxcommand.org/wss0090.php
https://bash.cyberciti.biz/guide/Returning_from_a_function
http://alvinalexander.com/linux-unix/shell-script-how-prompt-read-user-input-bash

Leveraging Tee and Grep for filtering program output

I have a utility which compiles my UI code and minifies them. It works pretty well and doesn’t requires much supervision. Trouble starts if the code compilation fails and it gets ignored in the huge log file which is generated by this utility. I use the following approach to filter out errors and yet preserving the output of the utility program for any diagnostic in case of any error.

./my_utility.sh | tee output.txt | grep error

Basically what happens is that “my_utility.sh” generates a lot of output which is piped into the “tee” command which in turn dumps it into a text file named “output.txt” and then the same output is again piped into the “grep” command which looks for any error text in the output. If the error is found it is outputted on the console.

So the end result is that in 99% cases I don’t see any output from the utility as I don’t want to see debug output. However the moment an error happens it gets flagged on console and I have the “output.txt” file to audit what went wrong and the root cause of the error.

Configuring Git user name and email

Since I like working using terminal I prefer to do all my operations of git from the command prompt. The first prerequisite to this is to have my name and email configured. This few commands are required to do this simple operation:

To set the name and email:

git config --global user.name "Your Name"
git config --global user.email "Your Email"

To view the name and email which is known to git execute the same commands without any parameter as shown below:

git config --global user.name
git config --global user.email