<p>I love using Go, and for scalability in the future I want to move toward using nginx as the web server.</p>
<p>Unfortunately I'm having an immense amount of trouble figuring out how to do this. I've found <a href="https://gist.github.com/hgfischer/7965620" rel="nofollow">a benchmark</a> that shows proxies over fastcgi are the way to go, but the specifics on doing it aren't clear. </p>
<p>I also found a tutorial on <a href="https://www.digitalocean.com/community/tutorials/how-to-use-martini-to-serve-go-applications-behind-an-nginx-server-on-ubuntu" rel="nofollow">how to do it on Digital Ocean</a> but it includes Martini that I don't need. </p>
<p>How should I be doing this?</p>
<hr/>**评论:**<br/><br/>jamra06: <pre><p>Just use the Digital Ocean link and ignore the Martini parts. It'll get you up and running. </p>
<p>The trick is to have a Go program running on the same port as specified in your nginx file.</p>
<pre><code>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:3000;
}
</code></pre>
<p>In this case, taken from your own DO link, run your Go program on port 3000. Now another thing to consider is to keep your Go program running. I use upstart, but you can also use systemd. </p></pre>captncraig: <pre><p>Nginx is really powerful, but can certainly be a pain to set up. I've migrated all of my personal servers to <a href="https://caddyserver.com" rel="nofollow">caddy</a> and couldn't be happier. Its all in go, and has a ton of cool bells and whistles (automatic https via let's encrypt is built in for example).</p></pre>curious2learn: <pre><p>Isn't caddy for static websites only? From their documentation it is not clear whether it can be used to serve a running Go program.</p></pre>alexwhoizzle: <pre><p>No, you can use it to proxy too: <a href="https://caddyserver.com/docs/proxy" rel="nofollow">https://caddyserver.com/docs/proxy</a></p></pre>kaeshiwaza: <pre><p>caddy just need to reach 1.0. I mean for stable api </p></pre>scottjbarr: <pre><p>You could just proxy to your backend servers. Haproxy is also a good frontend for your backend servers. You can also do nice stuff with service registration and discovery with a mix of haproxy, etcd, and your Go services. (See <a href="https://github.com/sohlich/etcd_service_discovery" rel="nofollow">https://github.com/sohlich/etcd_service_discovery</a>).</p>
<p>That may be more that you want of course so just configuring a couple of backend services might suit you. Either way, Nginx or Haproxy can do it.</p></pre>bonekeeper: <pre><blockquote>
<p>I've found a benchmark that shows proxies over fastcgi are the way to go, but the specifics on doing it aren't clear.</p>
</blockquote>
<p>The benchmarks you linked to show otherwise - that proxying using HTTP instead of TCP/Unix Socket was faster. Am I missing something?</p>
<p>Also, using nginx or Go directly doesn't have anything to do with "scalability", unless you're talking about using nginx as a load balancer (and not just a caching layer).</p></pre>hugocat: <pre><p>That's kinda what I was thinking, won't I need load balancing eventually?</p></pre>bonekeeper: <pre><p>Depends - if you host on AWS or Rackspace/etc, they have their own load balancing solutions, so you might or might not want to roll your own load balancing. They will use their hardware/etc to do the load balancing for you (remember you will need two machines at least for high availability) and they just forward the requests to your Go backends.</p>
<p>If you want to roll it out yourself you can use haproxy, nginx, or a combination of the two (if your balancing also has a lot of logic on it like URL rewriting, or if you want to have a caching layer as well for static content).</p></pre>hugocat: <pre><p>Hmm, so if I'm just using Digital Ocean something like haproxy will be needed eventually then?</p></pre>bonekeeper: <pre><p>Yes - two of them, ideally, so if one of them goes down, your website does not go down. <a href="https://www.digitalocean.com/community/tutorials/how-to-set-up-highly-available-haproxy-servers-with-keepalived-and-floating-ips-on-ubuntu-14-04" rel="nofollow">Something like this.</a></p></pre>hayzeus: <pre><p>First of all, I think what you are looking for is a load balancer; there is no need (ordinarily, anyway) to otherwise stick a web server in front of a go service; the http library should be fine.</p>
<p>Secondly, you should not use fastcgi. You will want to use a simple reverse proxy. Nginx and haproxy (which I kind of prefer for this purpose) will work just fine for this. There are other options as well, but I'm mainly familiar with these two (and ELB on aws).</p>
<p>Finally, you will want more than one load balancer, balanced using round-robin dns with the lowest ttl you can afford to pay for given your traffic.</p>
<p>I kind of get the feeling, though, that you may be putting the cart before the horse.</p></pre>bkeroack: <pre><p>You don't need nginx for scalability. The standard library (net/http) is extremely scalable by itself, up to thousands of req/sec, including TLS. </p></pre>bkeroack: <pre><p>lol @ being downvoted for saying what is very obviously true (I've proven it in production).</p></pre>hugocat: <pre><p>Won't I need load balancing though?</p></pre>sharptierce: <pre><p>yes, but you don't need nginx for that. There is also DNS load balancing and many other solutions depending on your environment. I actually use haproxy instead of nginx for LB. But it is more a LB specific question and does not have anything to do with golang. Maybe this helps: <a href="https://www.digitalocean.com/community/tutorials/how-to-configure-dns-round-robin-load-balancing-for-high-availability" rel="nofollow">https://www.digitalocean.com/community/tutorials/how-to-configure-dns-round-robin-load-balancing-for-high-availability</a></p></pre>hugocat: <pre><p>Thank you greatly. :) </p></pre>sharptierce: <pre><p>you're welcome :-)</p></pre>funny_falcon: <pre><p>Why you need nginx at all? Golang has great HTTP stack, it even serves static files great.</p>
<p>Single reason to use nginx is configuration flexibility.
But if your application doesn't need that flexibility, I'd rather not use nginx.</p></pre>hugocat: <pre><p>I'm afraid I'll need load balancing.</p></pre>funny_falcon: <pre><p>Between several servers? Then you already ought to know how use nginx to load balance for several upstream http servers. Do you know how to? If you do, then why you ask the question?</p></pre>funny_falcon: <pre><p>Also, Golang has utility function to make proxy requests, so you can easely build your own load balancer.</p></pre>curious2learn: <pre><p>Isn't nginx also helpful if hosting multiple applications on the same VPS?</p></pre>funny_falcon: <pre><p>All applications are written in Go?
I may suggest to compile all of them into one Go binary :-) it should work reasonably well for not heavy loaded server. Probably, it will work well for heavy loaded server too, but i'm not sure enough.</p>
<p>Also, Go has utility function to proxy http request in standard, so you can build your own proxy sever easely.</p></pre>
![](https://static.golangjob.cn/static/img/banner.png)