同一个服务器需要部署多个软件,但是80端口只能给一个,这时候我们需要使用反向代理。Nginx的反向代理相对简单,Apache也仅需简单配置即可。
这里特别介绍RewriteRule配置中的[P]标记,这是2.4版的新特性,所以在网络上很多的老旧资料中是找不着的。官方Wiki介绍(英文):http://httpd.apache.org/docs/2.4/rewrite/flags.html#flag_p
以本服务器为例,下面是ngrok的配置文件:
<VirtualHost *:80>
ServerName main.ngrok.skitisu.com
ServerAlias *.ngrok.skitisu.com
RewriteEngine On
RewriteRule ^/(.*) http://%{HTTP_HOST}:8000/$1 [P]
<Proxy *>
Order deny,allow
Allow from all
</Proxy>
ProxyPass / http://localhost:8000/
ProxyPassReverse / http://localhost:8000/
</VirtualHost>
对于ASP.NET WEB API程序,我需要将/跳转到index.html但保持网址不变,配置如下:
<VirtualHost *:80>
ServerName numgame.skitisu.com
RewriteEngine On
# 判断语句,如果请求的参数为/
RewriteCond %{REQUEST_URI} =/
# 那么反代到后端软件的/index.html
RewriteRule / http://localhost:5000/index.html [P]
# 否则正常传递请求参数反代
RewriteRule ^/(.*) http://localhost:5000/$1 [P]
<Proxy *>
Order deny,allow
Allow from all
</Proxy>
ProxyPass / http://localhost:5000/
ProxyPassReverse / http://localhost:5000/
</VirtualHost>
历史引用
- 在已有apache服务的VPS上安装ngrok实现远程管理路由器 – 草莓LEDE · 2017-03-14 11:35:03.000000
[…] 在所需的域名里做代理转发到ngrok代码如下: 地址在这里:https://blog.skitisu.com/apache-config-reverse-proxy […]
查看引用来源
END OF RECORD返回顶部 ↑