根据url参数代理到不同的虚拟主机中
TIP:正向代理、反向代理以及url重写
- 正向代理:正向代理是一个位于客户端和目标服务器之间的代理服务器(中间服务器)。为了从原始服务器取得内容,客户端向代理服务器发送一个请求,并且指定目标服务器,之后代理向目标服务器转交并且将获得的内容返回给客户端。
- 反向代理:反向代理正好相反。对于客户端来说,反向代理就好像目标服务器。并且客户端不需要进行任何设置。客户端向反向代理发送请求,接着反向代理判断请求走向何处,并将请求转交给客户端,使得这些内容就好似他自己一样,一次客户端并不会感知到反向代理后面的服务,也因此不需要客户端做任何设置,只需要把反向代理服务器当成真正的服务器就好了。
- url重写:url重写就是首先获得一个进入的URL请求然后把它重新写成网站可以处理的另一个URL的过程。举个例子来说,如果通过浏览器进来的URL是“UserProfile.aspx?ID=1”那么它可以被重写成 “UserProfile/1.aspx”,这样的URL,这样的网址可以更好的被网站所阅读。
url为:https://domain/1915/?c=user&a=Add
需求:根据?后的c=user&a=Add进行匹配,从而进入不同的虚拟主机中
注意:此处url中的1915是authid,authid是动态生成,所以需要考虑到该问题(使用正则解决即可)。
apache
实现技术:使用了apache的RewriteRule重定向到指定的url中,具体配置如下:
# Python配置
WSGIPythonHome /usr/local/python3
<VirtualHost *:9052>
SSLEngine on
SSLCertificateFile "/usr/local/mawbd/conf/server.crt"
SSLCertificateKeyFile "/usr/local/mawbd/conf/server.key"
WSGIScriptAlias / /yourpath
<Directory /yourpath>
AllowOverride None
Order allow,deny
Allow from all
Options All
Require all granted
</Directory>
</VirtualHost>
# 反向代理(python)
<VirtualHost *:443>
SSLEngine on
SSLCertificateFile "/yourpath"
SSLCertificateKeyFile "/yourpath"
<FilesMatch \.php$>
SetHandler "proxy:fcgi://127.0.0.1:9000"
</FilesMatch>
DocumentRoot "/yourpath"
<Directory "/yourpath">
Options FollowSymLinks
AllowOverride All
Require all granted
</Directory>
# 开启反向代理ssl模块
SSLProxyEngine on
SSLProxyCheckPeerCN Off
SSLProxyCheckPeerName Off
Proxyrequests off
# 以下为配置重定向到Python中
RewriteEngine on # 开启rewrite功能
RewriteCond %{REQUEST_URI} ^/[0-9]{1,4} #根据请求的url正则匹配1-4位字符
RewriteCond %{QUERY_STRING} ^c=(user&a=Add*) #根据?后的参数进行匹配
RewriteRule ^(.*) https://127.0.0.1:9052/1234/ [P] #[p] 为反向代理
</VirtualHost>
nginx
实现技术:使用了nginx的$request_uri
进行正则匹配
location ~ \.php {
root htdocs;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
set $real_script_name $fastcgi_script_name;
if ($fastcgi_script_name ~ "^(.+?\.php)(/.+)$") {
set $real_script_name $1;
set $path_info $2;
}
# 以下为重定向到Python中
if ($request_uri ~* /\d+/\?c\=user\&a=Add){ #进行正则匹配
proxy_pass https://127.0.0.1:9001/$request_uri; #反向代理到Python中
break;
}
fastcgi_param SCRIPT_FILENAME $document_root$real_script_name;
fastcgi_param SCRIPT_NAME $real_script_name;
fastcgi_param PATH_INFO $path_info;
}
}
lighttpd
实现技术:通过lighttpd服务器反向代理技术,对整体请求进行二次识别,对url识别后,使用函数对参数进行识别,从而根据不同的url以及参数的变量进入到Python中,从而实现PHP+Python
$SERVER["socket"] == "0.0.0.0:443" {
ssl.engine = "enable"
ssl.pemfile = "/yourpath"
ssl.ca-file = "/yourpath"
server.document-root = "/yourpath"
$HTTP["url"] =~ "/" {
fastcgi.server = (
".php" => ((
"host" => "127.0.0.1",
"port" => "9000"
))),
$HTTP["querystring"] =~ "^/?c=user&a=Add"{
proxy.server = (
"" => ((
"host" => "127.0.0.1",
"port" => 9088
)))
}
}
url.rewrite-once = (
"^/(\d*)/(.*)$" => "/$2&authid=$1",
)
}
# python 虚拟主机
$SERVER["socket"] == "0.0.0.0:9088"{
# lighttpd官方文档提示,所被代理的服务器不支持SSL
# ssl.engine = "enable"
# ssl.pemfile = "/yourpath"
# ssl.ca-file = "/yourpath"
server.document-root = "/yourpath"
$HTTP["url"] !~ "^/static" {
fastcgi.server = ("/" =>
((
"socket" => "/tmp/myapp-fcgi.sock",
"bin-path" => "/yourpath",
"check-local" => "disable",
"max-procs" => 1
))
)
# alias.url=("/"=>"/")
# here Solve the problem of URL redundant parameters
url.rewrite-once = (
"^/(.*)&authid=(\d*)$"=>"/runapp.fcgi/$2/$1", # 这里将url重写进行写回
)
}
}
lighttpd官方文档解释如下: