Normally, TCP exclusively listens on a single port, but a machine may need to host multiple HTTP servers. Having two services simultaneously listen on port 80 is technically possible with certain parameters, but modifying the programs to avoid conflicts is quite troublesome.

Typically, we can use Apache or Nginx as a reverse proxy, listening on ports 80 and 443, and then forward HTTP requests based on the requested host.

A simple configuration is as follows:

Bash

# 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

The above configuration can be placed in the /etc/nginx/conf.d/ directory, because the default Nginx configuration file, typically /etc/nginx/nginx.conf, will include all files ending with .conf in this directory. Therefore, we can save each domain as a separate file, making modifications much simpler. On Windows, you can also consider pasting this content directly inside http { }.

Then, use

Bash
# sudo nginx -t
to test whether the current configuration is correct; if there are errors, follow the prompts to fix them.

Finally, use

Bash
# sudo service nginx reload
to reload the Nginx configuration.