一般情况下,TCP 是独占监听一个端口的,但是一台机器上可能需要部署若干个 HTTP Server,两个服务程序同时监听 80 端口,虽然可以通过一些参数做到,不过修程序让它们不打架相当麻烦。

一般情况下我们可以通过 Apache 或者 Nginx 作为连接服务,监听 80 和 443 端口,然后按请求 host 分别转发 http 请求。

一个简单的配置如下:


# http {
server {
    listen 80; # 监听80 端口, http 默认端口
    server_name agent-host-zero agent-host-one; # 匹配的域名,只有这些域名才会采用这个 server 下的配置。支持泛域名,多个域名用空格隔开即可。

    location / {
        # If you use https make sure you disable gzip compression
        # to be safe against BREACH attack
        # gzip off;
        
        client_max_body_size 20M; # limit http body size in request
        
        # as name
        proxy_read_timeout 300; # Some requests take more than 30 seconds.
        proxy_connect_timeout 300; # Some requests take more than 30 seconds.
        proxy_redirect     off;
        
        proxy_set_header X-Real-IP  $remote_addr; # forward real client ip, X-Real-IP can also assigned as other header key
        proxy_set_header   X-Forwarded-Proto $scheme;
        proxy_set_header   X-Forwarded-For   $proxy_add_x_forwarded_for;
        
        # 这行会修改 http 的头,把 host 改成你需要。若用 $http_host 表示会follow 用户的 host, 若用 "real-host-name" 表示自定义
        #proxy_set_header   Host              $http_host; # this line will send real host name, e.g. agent-host-zero
        proxy_set_header   Host              "real-host-name";

        # 注意,下面一行的 real-host-name-or-ip 可以是域名或者 IP, 但无论如何,这个地址只是指向源的地址,和 http 的 request 报文没有任何关系。
        proxy_pass http://real-host-name-or-ip:80/;
    }
}

# } # http

如上配置可以放在 /etc/nginx/conf.d/ 目录下,因为一般 /etc/nginx/nginx.conf ,这个 nginx 默认的配置文件会引用这个目录下所有.conf 为后缀的文件。因此我们可以把每个域名分别保存为单独的文件,修改就会比较简单。 若在 windows 下,也可以考虑直接把这段内容贴在 http{ } 中。

然后,用

# sudo nginx -t

可以测试当前配置是否成功,有错误按照提示再修改下。

最后,用

# sudo service nginx reload

刷新下 nginx 的配置即可。

Categories: Code

Yu

Ideals are like the stars: we never reach them, but like the mariners of the sea, we chart our course by them.

7 Comments

tor · May 12, 2016 at 22:28

Google Chrome 49.0.2623.87 Google Chrome 49.0.2623.87 Mac OS X  10.11.4 Mac OS X 10.11.4

SATISFY YOUR VANITY

灰姑娘 · April 26, 2016 at 10:21

Google Chrome 49.0.2623.110 Google Chrome 49.0.2623.110 Windows 10 x64 Edition Windows 10 x64 Edition

通过慎重考虑、反复的思想斗争,我还是决定提键盘满足下楼主的虚荣~

    yu · April 26, 2016 at 11:11

    Google Chrome 50.0.2661.86 Google Chrome 50.0.2661.86 Mac OS X  10.11.4 Mac OS X 10.11.4

    @灰姑娘 点点点

reizhi · April 22, 2016 at 21:10

Google Chrome 50.0.2661.75 Google Chrome 50.0.2661.75 Windows 10 x64 Edition Windows 10 x64 Edition

教程写得很明白

    yu · April 23, 2016 at 12:23

    Google Chrome 49.0.2623.112 Google Chrome 49.0.2623.112 Mac OS X  10.11.4 Mac OS X 10.11.4

    @reizhi XD, 这个是有目的的写给两个人,省的我一遍又一遍说的。
    注意到你也写了个类似的。

youran · April 21, 2016 at 00:49

Google Chrome 49.0.2623.112 Google Chrome 49.0.2623.112 Windows 10 x64 Edition Windows 10 x64 Edition

可以用这种方法反代自己的网站,配合cloudxns搞个self-hosted CDN

    yu · April 21, 2016 at 07:37

    Google Chrome 49.0.2623.112 Google Chrome 49.0.2623.112 Mac OS X  10.11.4 Mac OS X 10.11.4

    @youran
    学习了下,机智!
    我目前只是用它在用一台公网反代若干服务。然后把其它服务全部藏在内网。

Leave a Reply to yu Cancel reply

Your email address will not be published. Required fields are marked *