/ WhatILearned

nginx config entry for serving a go app

While you run nginx to quickly serve your static files sometimes you need to serve dynamic content such as PHP scripts or Go binaries on the same server.

Go is not the same as PHP. Go is compiled into an executable (like a program) whereas another program (think like PHP Engine) executes php scripts on the fly. When you compile things into an executable not only do you get a boost in speed, compilation performs a bunch of checks to help ensure that things will work in the way intended. Scripts like PHP will continue on and make a lot of assumptions which are convenient but can result in unexpected results.

I'm only going to cover what you need to do on the nginx side of things. This doesn't cover setting up the server to automatically start the go binary on startup.

Assuming that you've successfully compiled your Go Web app and you know what port it's listening on here's the minimum working entry that you'll need to add to your config file in the sites-available and sites-enabled. Assuming the port number that Go app is listening to is 4356

server {
    # Make site accessible from http://goapp.domain.com/
    server_name goapp.domain.com;

    location / {
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $remote_addr;
        proxy_set_header Host $host;