首页 > Go开发网站, 多域名部署问题

Go开发网站, 多域名部署问题

问题是这样的:
我用Go开发了2个网站(完全不同的网站),例如, www.a.com 和 www.b.com , 现在要把这2个网站同时部署在一台服务器上。 但80端口只能一个网站占用。
a网站: http.ListenAndServe(":80", nil)
b网站:只能用其他端口了http.ListenAndServe(":81", nil)。 那么访问b网站,只能www.b.com:81

有没有办法同时80端口?请求根据域的不同, 声明,这2个网站是完全不同客户的网站,但部署在同一服务器上。

或者前端需要加上Nginx来帮助?

感谢解答, 谢谢


如果修改nginx的配置,在你知道几个网站的情况下是最好的;
我们采用的是获得 $_SERVER['HTTP_HOST'],去判断不同的网站,因为域名不一样,这样的好处是不用去重新启动nginx的服务,再添加一个域名的情况下。


何必要Nginx或者Apache这么麻烦,Go自己就可以做独立的服务器,虚拟主机自然自己就支持
http://golang.org/pkg/net/http/#Serve...
ServeMux is an HTTP request multiplexer. It matches the URL of each incoming request against a list of registered patterns and calls the handler for the pattern that most closely matches the URL.
Patterns may optionally begin with a host name, restricting matches to URLs on that host only. Host-specific patterns take precedence over general patterns, so that a handler might register for the two patterns "/codesearch" and "codesearch.google.com/" without also taking over requests for "http://www.google.com/".

所以,注册handler的时候加上域名即可
http.HandleFunc("www.a.com/", handlerFunc)


在你的nginx通过代理的方式转发请求:配置如下

server {
        listen       80;
        server_name  www.a.com;
        charset utf-8;
        access_log  /home/a.com.access.log  main;
        location / {
            proxy_pass http://127.0.0.1:80;
        }
    }

 server {
        listen       80;
        server_name  www.b.com;
        charset utf-8;
        access_log  /home/b.com.access.log  main;
        location / {
            proxy_pass http://127.0.0.1:81;
        }
    }

你可以 apache,这个可以在同一台服务器上部署N个网站,只要你的空间和内存够用就可以了


每个网站都应该有自己的IP地址,然后监听不同IP地址就可以了。
用nginx或lighttpd代理的缺点是无法准确获知客户端的IP地址。

【热门文章】
【热门文章】