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
+7.9k Javascript : How to check a browser's Do Not Track status?
+8.4k Your page has meta tags in the body instead of the head
+6.3k Javascript : Generate random key with specific length
+19.5k Golang : How to Set or Add Header http.ResponseWriter?
+5.3k Golang : How to deal with configuration data?
+7.7k Golang : Error reading timestamp with GORM or SQL driver
+28k Golang : Move file to another directory
+4.7k Chrome : How to block socketloop.com links in Google SERP?
+8.6k Golang : Add text to image and get OpenCV's X, Y co-ordinates example
+19.9k Golang : Measure http.Get() execution time
+19.9k Golang : How to get time from unix nano example