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

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

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配置中指向它们:

  1. server {
  2. listen 80;
  3. server_name turalasgar.pro;
  4. location / {
  5. proxy_pass http://localhost:8080;
  6. ...
  7. }
  8. }
  9. server {
  10. listen 80;
  11. server_name engossip.com;
  12. location / {
  13. proxy_pass http://localhost:8081;
  14. ...
  15. }
  16. }
英文:

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

  1. server {
  2. listen 80;
  3. server_name turalasgar.pro;
  4. location / {
  5. proxy_pass http://localhost:8080;
  6. ...
  7. }
  8. }
  9. server {
  10. listen 80;
  11. server_name engossip.com;
  12. location / {
  13. proxy_pass http://localhost:8081;
  14. ...
  15. }
  16. }

答案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

  1. server {
  2. listen ...;
  3. ...
  4. location / {
  5. proxy_pass http://127.0.0.1:8080;
  6. }
  7. location /blog {
  8. rewrite ^/blog(.*) /$1 break;
  9. proxy_pass http://127.0.0.1:8181;
  10. }
  11. location /mail {
  12. rewrite ^/mail(.*) /$1 break;
  13. proxy_pass http://127.0.0.1:8282;
  14. }
  15. ...

}

英文:

Above solutions I tried but didn't work for me

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

  1. server {
  2. listen ...;
  3. ...
  4. location / {
  5. proxy_pass http://127.0.0.1:8080;
  6. }
  7. location /blog {
  8. rewrite ^/blog(.*) /$1 break;
  9. proxy_pass http://127.0.0.1:8181;
  10. }
  11. location /mail {
  12. rewrite ^/mail(.*) /$1 break;
  13. proxy_pass http://127.0.0.1:8282;
  14. }
  15. ...

}

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:

确定