使用Nginx部署多个Go应用程序

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

Deploying multiple Go applications using Nginx

问题

有两个使用Go编写的Web应用程序(网站)。一个是turalasgar.pro(我在这里使用Go内置服务器)。另一个是engossip.com(目前显示与前者相同的IP)。我有一个VPS。我知道我应该使用Nginx,但不知道该怎么做?我听说过Caddy。请注意,我只需要Nginx服务器,不需要Caddy。我需要的是通过使用同一个VPS运行两个(或更多)应用程序。我应该如何配置Nginx配置?是通过监听不同的端口还是同一个端口?非常感谢实际的建议和示例。

英文:

The are two web applications (websites) written on Go. One is turalasgar.pro (here I am using Go built-in server). Another is engossip.com (for now it displays the same ip as former). I have a vps. I know I should use Nginx, but have no idea how? I have heard of Caddy. Please, I need only nginx server, not Caddy. What I need is run two (or more) applications by using my same vps. How should I configure Nginx configuration? Whether by listening to different ports or to the same port. Practical advices and examples highly appreciated.

答案1

得分: 6

这被称为反向代理。每个应用程序使用自己的端口进行监听。然后你只需要在nginx配置中指向它们:

server {
    listen 80;
    server_name turalasgar.pro;
    location / {
        proxy_pass http://localhost:8080;
        ...
    }
}

server {
    listen 80;
    server_name engossip.com;
    location / {
        proxy_pass http://localhost:8081;
        ...
    }
}
英文:

It's called reverse proxy. Each application uses it's own port to listen. And then you just point to them in nginx config:

server {
    listen 80;
    server_name turalasgar.pro;
    location / {
        proxy_pass http://localhost:8080;
        ...
    }
}

server {
    listen 80;
    server_name engossip.com;
    location / {
        proxy_pass http://localhost:8081;
        ...
    }
}

答案2

得分: 1

好的,以下是翻译好的内容:

很简单。

按照这个指南操作:

https://www.digitalocean.com/community/tutorials/how-to-use-martini-to-serve-go-applications-behind-an-nginx-server-on-ubuntu

在你成功使用martini+nginx运行一个应用程序之后,只需为另一个应用程序添加一个server块。

如果你需要更多关于server blocks的信息:

https://www.digitalocean.com/community/tutorials/how-to-set-up-nginx-server-blocks-virtual-hosts-on-ubuntu-14-04-lts

英文:

Well is really easy.

follow this guide:

https://www.digitalocean.com/community/tutorials/how-to-use-martini-to-serve-go-applications-behind-an-nginx-server-on-ubuntu

After you achieved one application working with martini+nginx just add another server block for the other app.

In case you need more information about server blocks:

https://www.digitalocean.com/community/tutorials/how-to-set-up-nginx-server-blocks-virtual-hosts-on-ubuntu-14-04-lts

答案3

得分: 0

我试过上述的解决方案,但对我没有起作用。

> https://gist.github.com/soheilhy/8b94347ff8336d971ad0

server {
listen       ...;
...
location / {
    proxy_pass http://127.0.0.1:8080;
}

location /blog {
    rewrite ^/blog(.*) /$1 break;
    proxy_pass http://127.0.0.1:8181;
}

location /mail {
    rewrite ^/mail(.*) /$1 break;
    proxy_pass http://127.0.0.1:8282;
}
...

}

英文:

Above solutions I tried but didn't work for me

> https://gist.github.com/soheilhy/8b94347ff8336d971ad0

server {
listen       ...;
...
location / {
    proxy_pass http://127.0.0.1:8080;
}

location /blog {
    rewrite ^/blog(.*) /$1 break;
    proxy_pass http://127.0.0.1:8181;
}

location /mail {
    rewrite ^/mail(.*) /$1 break;
    proxy_pass http://127.0.0.1:8282;
}
...

}

huangapple
  • 本文由 发表于 2017年8月7日 04:09:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/45536166.html
匿名

发表评论

匿名网友

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

确定