Does anyone know of any tutorials for using nginx efficiently as the web server for Golang?

xuanbao · · 694 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<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&#39;m having an immense amount of trouble figuring out how to do this. I&#39;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&#39;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&#39;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&#39;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&#39;ve migrated all of my personal servers to <a href="https://caddyserver.com" rel="nofollow">caddy</a> and couldn&#39;t be happier. Its all in go, and has a ton of cool bells and whistles (automatic https via let&#39;s encrypt is built in for example).</p></pre>curious2learn: <pre><p>Isn&#39;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&#39;ve found a benchmark that shows proxies over fastcgi are the way to go, but the specifics on doing it aren&#39;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&#39;t have anything to do with &#34;scalability&#34;, unless you&#39;re talking about using nginx as a load balancer (and not just a caching layer).</p></pre>hugocat: <pre><p>That&#39;s kinda what I was thinking, won&#39;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&#39;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&#39;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&#39;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&#39;ve proven it in production).</p></pre>hugocat: <pre><p>Won&#39;t I need load balancing though?</p></pre>sharptierce: <pre><p>yes, but you don&#39;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&#39;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&#39;t need that flexibility, I&#39;d rather not use nginx.</p></pre>hugocat: <pre><p>I&#39;m afraid I&#39;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&#39;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&#39;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>

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

694 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传