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
+4.5k Chrome : How to block socketloop.com links in Google SERP?
+21.6k Golang : Join arrays or slices example
+7.3k Android Studio : AlertDialog to get user attention example
+14k Golang : How to convert a number to words
+6.3k Golang : Combine slices of complex numbers and operation example
+10.1k Fix ERROR 1045 (28000): Access denied for user 'root'@'ip-address' (using password: YES)
+7.3k Golang : Get YouTube playlist
+29.5k Golang : How to declare kilobyte, megabyte, gigabyte, terabyte and so on?
+39.7k Golang : Convert to io.ReadSeeker type
+9.5k Golang : Turn string or text file into slice example
+16.2k Golang : Get IP addresses of a domain name
+12.4k Golang : Sort and reverse sort a slice of bytes