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
+11.9k Golang : md5 hash of a string
+18.9k Golang : Check if directory exist and create if does not exist
+16.1k Golang : Convert slice to array
+9.1k Golang : Find the length of big.Int variable example
+19.3k Golang : How to Set or Add Header http.ResponseWriter?
+16.4k Golang : Gzip file example
+9.1k Golang : Timeout example
+21.8k Golang : Convert seconds to minutes and remainder seconds
+16.3k Golang : Merge video(OpenCV) and audio(PortAudio) into a mp4 file
+7.3k Golang : Dealing with struct's private part
+14.8k Golang : Save(pipe) HTTP response into a file