How to run a Go http server with nginx

huangapple go评论80阅读模式
英文:

How to run a Go http server with nginx

问题

我有一个用Go语言编写的简单HTTP服务器。

在开发环境中,它运行良好,但是在生产环境中,当这个服务器需要同时处理100个请求时,我需要一个像nginx这样的合适的Web服务器。

我该如何将它放在nginx后面?

英文:

I have a simple HTTP server written in Go.

In development It works fine but for production, where this server has to handle 100 requests at a time I need a proper web server like nginx.

How can I put it behind nginx?

答案1

得分: 15

我猜你需要一个简单的反向代理配置。

假设你的Go HTTP服务器监听在 http://example.com:8080 上:

server {
    listen 80;
    server_name example.com;

    location / {
        proxy_pass http://example.com:8080;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_http_version 1.1;
        proxy_set_header Connection "";
    }
}
英文:

I'm guessing you need a simple reverse proxy config.

Lets say your go http server is listening on http://example.com:8080 :

server {
    listen 80;
    server_name example.com;

    location / {
        proxy_pass http://example.com:8080;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_http_version 1.1;
        proxy_set_header Connection "";
    }
}

huangapple
  • 本文由 发表于 2017年3月4日 12:53:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/42592238.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定