1.场景:
通过test.open.com访问81服务器指定的8081端口
2.应用:
2.1首先是在云服务器(这里用了阿里云)去配置一个test.open.cn的域名;
2.2在81这台服务器安装nginx,并在nginx.conf进行配置,配置实例如下:
access_log /web/logs/access.log main; sendfile on; #tcp_nopush on; upstream tomcat_server { server xxx.xx.xxx.81:8081 weight=1; } #keepalive_timeout 0; keepalive_timeout 65; #gzip on; server { listen 80; server_name test.open.cn; charset utf-8; #access_log logs/host.access.log main; location / { root html; index index.html index.htm; proxy_pass http://tomcat_server; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root html; }
解释下:当发送http请求http://test.open.cn,首先是根据域名test.open.cn定位到xxx.xx.xxx.81这台服务器,然后在这台服务器上安装了nginx,并对80端口进行了监听(上面高亮字),当域名为test.open.cn的请求到来的时候,就会
将改请求定位到location,而location里的属性proxy_pass,有tomcat_server(上面upstream定位为xxx.xx.xxx.81),最后就是转化为http:xxx.xx.xxx.81这个请求了。