Golang : Configure Apache and NGINX to access your Go service example



Tags : golang-service apache2 nginx reverse-proxy virtual-host

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.

  1. Install the proxy and proxy_http modules (e.g. sudo a2enmod proxy on an Ubuntu server)
  2. Add the virtual host configuration (see below)
  3. 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

https://flaviocopes.com/go-nginx-reverse-proxy/

https://www.sitepoint.com/community/t/problem-trying-to-run-golang-with-apache2-as-a-reverse-proxy-server/212704/3

Affiliate :

Hire Golang Developers


  See also : Golang : Get Hokkien(福建话)/Min-nan(閩南語) Pronounciations



Tags : golang-service apache2 nginx reverse-proxy virtual-host

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