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条)

报告相同问题?

悬赏问题

  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?