Golang : Configure Apache and NGINX to access your Go service example
Recently I developed two services that allows translation of Bahasa Malaysia in Roman alphabets to Jawi(calligraphy form) and getting the correct Hokkien(Min-nan) pronunciations from a given Hanzi input.
Both of these services need to be exposed online instead of just being available on localhost. The thing is that this website was made with PHP and served via NGINX. So, how to expose the services and make them available worldwide?
For NGINX, just add a reverse proxy for the Go service. For example:
server {
listen 80 default_server;
server_name your-domain;
location /rumitojawi {
proxy_set_header X-Real-IP $remote_addr;
proxy_pass http://localhost:8001; // make sure your production server's Firewall allow traffic to this port
}
location /hokkienminnan {
proxy_set_header X-Real-IP $remote_addr;
proxy_pass http://localhost:8002; // make sure your production server's Firewall allow traffic to this port
}
location /go-what-service {
proxy_set_header X-Real-IP $remote_addr;
proxy_pass http://localhost:8003; // make sure your production server's Firewall allow traffic to this port
}
}
The proxy_set_header X-Real-IP $remote_addr
parameter is to instruct NGINX to pass the visitor’s real IP address. Your Go service can capture the address with
http.Request.Header.Get("X-Real-IP")
As for Apache2 server, I haven't tested it, but the following instruction should be helpful in getting Apache2 to reverse proxy requests to your Golang service.
Make sure that RewriteEngine On
is there, otherwise the configuration will not work.
- Install the proxy and proxy_http modules (e.g. sudo a2enmod proxy on an Ubuntu server)
- Add the virtual host configuration (see below)
- Restart apache2
The VirtualHost settings:
<VirtualHost *:80>
#RewriteBase "/products/"
#RewriteRule "^widget/(.*)$" "http://product.example.com/widget/$1" [P]
#ProxyPassReverse "/products/widget/" "http://product.example.com/widget/"
RewriteEngine On
RewriteRule "^rumitojawi(.*)$" "http://example.com/rumitojawi$1" [P]
ProxyPassReverse "/rumitojawi" "http://example.com/rumitojawi"
ServerName www.example.com
ProxyPass / http://127.0.0.1:8001/
</VirtualHost>
For more details, please see how to configure Apache2 reverse proxy at https://httpd.apache.org/docs/2.4/howto/reverse_proxy.html
By the way, if your main PHP application used routing to map to the different controller's name. Remember to configure the routing table as well, otherwise you will not be able to expose your Golang application properly. Be extra careful when using * wildcard in your regular expression.
Happy coding!
References :
https://www.facebook.com/groups/206770519471402/1341956832619426/?comment_id=1341969462618163
See also : Golang : Get Hokkien(福建话)/Min-nan(閩南語) Pronounciations
By Adam Ng(黃武俊)
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+14.5k Golang : Find commonalities in two slices or arrays example
+5.2k Golang *File points to a file or directory ?
+5k Golang : Calculate half life decay example
+5.4k Golang : Frobnicate or tweaking a string example
+13.4k Golang : Image to ASCII art example
+9k Golang : Generate random Chinese, Japanese, Korean and other runes
+47.3k Golang : Convert int to byte array([]byte)
+7.1k Golang : Of hash table and hash map
+7.3k Golang : How to handle file size larger than available memory panic issue
+8.8k Golang : Handle sub domain with Gin
+6.5k Golang : When to use make or new?
+12k Golang : Simple client-server HMAC authentication without SSL example