Golang nginx reverse proxy for ubuntu

If you have configured your golang application to run using upstart the best way to serve it to the world is behind a reverse proxy on a single server instance or haproxy using mutiple servers. To run an a golang application on a single server listening on port 8080 you will need to use nginx to proxy requests from the standard port 80.

To install nginx on ubuntu first ensure that apache is not installed on the server and then install nginx

sudo apt-get remove apache*
sudo apt-get install nginx

Now change the default configuration which is located at /etc/nginx/sites-available/default with the following configuration:

server {
	listen 80 default_server;
	listen [::]:80 default_server ipv6only=on;

	server_name sleepy.fish;

	location / {
		proxy_set_header X-Real-IP $remote_addr;
		proxy_set_header X-Forwarded-For $remote_addr;
		proxy_set_header Host $host;
		proxy_pass http://127.0.0.1:8000;
	}
}

You will need to swap the server name sleepy.fish to either be the name of the server or localhost. To start the server for the first time or to restart the server run:

sudo service nginx restart

You should then be able to access your application via http://{server-ip}/


See also