在部署非ApacheNginxWeb应用服务时,需部署在特定端口,如Tomcat应用便通常部署在8080端口,访问此类服务时,需在域名或IP后跟端口名,很不雅观,可通过Apache的反向代理服务将其它端口代理至80443端口,实现不用接端口号即可访问相应内容。以自建Leanote笔记服务为例,其部署在9000端口,正常链接为wteng.top:9000,通过设置apache反向代理,配合二级域名,来实现将访问链接设为note.wteng.top。环境Ubuntu

二级域名设置

新建二级域名解析记录,指向服务器IP地址,记录类型为A记录:
Inkedgit_LI.jpg

开启代理模块

开启命令:

a2enmod proxy proxy_http

80端口反向代理设置

http://note.wteng.top链接反向代理至http://wteng.top:9000,只需编辑/etc/apache2/sites-available下配置文件:

vi /etc/apache2/sites-available/000-defalut.conf

加入以下一条虚拟主机配置:

<VirtualHost note.wteng.top:80>        #新建虚拟主机
    ServerName note.wteng.top    #虚拟主机名,即域名
    ServerAdmin my@wteng.top    #管理员
    ProxyPass    / http://localHost:9000/    #匹配的url,一切来自note.wteng.top的请求都会转发至该url处理
    ProxyPassReverse    / http://localhost:9000/    #url返回的一切信息,都会重新包装成note.wteng.top域名形式返回
</VirtualHost>

重新加载配置:

service apache2 reload

443端口反向代理设置

443端口是https所用的端口,若服务器有ssl设置,即可通过https访问,需进行此端口的反向代理设置。
首先进入/etc/apache2/sites-available目录,新建站点配置文件:

cd /etc/apache2/sites-available
vi 001-default.conf

加入以下配置:

<VirtualHost note.wteng.top:443>    #新建虚拟主机
    ServerName note.wteng.top    #主机名
    ServerAdmin my@wteng.top    #管理员
    SSLEngine on    #开启SSL
    SSLCertificateFile "/etc/apache2/ssl/server.pem"    #SSL证书文件位置
    SSLCertificateKeyFile "/etc/apache2/ssl/server.key"        #SSL密钥文件位置
    ProxyPass    / http://localhost:9000/    #匹配的url,一切来自https://note.wteng.top的请求都会转发至该url处理
    ProxyPassReverse    http://localhost:9000/    #url返回的一切信息,都会重新包装成https://note.wteng.top域名形式返回
</VirtualHost>

重新加载配置:

service apache2 reload
经过以上配置,访问Leanote时,不必再通过wteng.top:9000ip:9000的形式进行访问,而是直接通过note.wteng.tophttps://note.wteng.top的形式访问即可。