Golang + Gin + Nginx

  • api.gin-sample.com にアクセスすると前提
  • /path/to/go/src/your/project に main.go を置いている前提
$ cd /etc/nginx/conf.d
$ vi gin-sample.conf

gin-sample.conf

upstream gin_sample {
   server localhost:8080;
}

server {
    listen       80;
    server_name  api.gin-sample.com;

    root /path/to/go/src/your/project;

    proxy_redirect                          off;
    proxy_set_header Host                   $host;
    proxy_set_header X-Real-IP              $remote_addr;
    proxy_set_header X-Forwarded-Host       $host;
    proxy_set_header X-Forwarded-Server     $host;
    proxy_set_header X-Forwarded-For        $proxy_add_x_forwarded_for;

    location / {
        proxy_pass http://gin_sample/;
    }

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
}

main.go 起動

$ cd /path/to/go/src/your/project
$ go run main.go

もしくは