drryyiuib43562604 2015-09-03 22:31
浏览 45
已采纳

在Web应用程序中将Web服务器与Nginx服务器一起使用[重复]

This question already has an answer here:

Sorry, I cannot find this answer from Google search and nobody seems to explain clearly the difference between pure Go webserver and nginx reverse proxy. Everybody seems to use nginx in front for web applications.

My question is, while Go has all http serving functions, what is the benefit of using nginx over pure Go web servers?

And in most cases, we set up the Go webserver for all routes here and have the nginx configurations in front.

Something like:

limit_req_zone $binary_remote_addr zone=limit:10m rate=2r/s;

server {
    listen 80;

    log_format lf '[$time_local] $remote_addr ;

    access_log /var/log/nginx/access.log lf;
    error_log /var/log/nginx/error.log;

    set_real_ip_from 0.0.0.0/0;
    real_ip_header X-Forwarded-For;
    real_ip_recursive on;
    server_name 1.2.3.4 mywebsite.com;
}

When we have this Go:

func handler(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintf(w, "Hi there, I love %s!", r.URL.Path[1:])
}

func main() {
    http.HandleFunc("/", handler)
    http.ListenAndServe(":8080", nil)
}

Are the traffic to nginx and Go web server different? If not, why do we have two layers of web server?

Please help me understand this.

Thanks,

</div>
  • 写回答

3条回答 默认 最新

  • douwen3500 2015-09-03 23:08
    关注

    There's nothing stopping you from serving requests from Go directly.

    On the other hand, there are some features that nginx provides out-of-the box that may be useful, for example:

    • handle many virtual servers (e.g. have go respond on app.example.com and a different app on www.example.com)

    • http basic auth in some paths, say www.example.com/secure

    • access logs

    • etc

    All of this can be done in go but would require programming, while in nginx it's just a matter of editing a .conf file and reloading the configuration. Nginx doesn't even need a restart for this changes to take place.

    (From a "process" point of view, nginx could be managed by an ops employee, with root permissions, running in a well known port, while developers deploy their apps on higher ones.)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题