Web服务器根据url参数代理

根据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中,具体配置如下:

 1 # Python配置
 2 
 3 WSGIPythonHome /usr/local/python3
 4 <VirtualHost *:9052>
 5     SSLEngine on
 6     SSLCertificateFile "/usr/local/mawbd/conf/server.crt"
 7     SSLCertificateKeyFile "/usr/local/mawbd/conf/server.key"
 8     
 9     WSGIScriptAlias / /yourpath
10     <Directory /yourpath>
11         AllowOverride None
12         Order allow,deny  
13         Allow from all  
14         Options All
15         Require all granted 
16     </Directory>
17 </VirtualHost>
18 
19 # 反向代理(python)
20 <VirtualHost *:443>
21     SSLEngine on
22     SSLCertificateFile "/yourpath"
23     SSLCertificateKeyFile "/yourpath"
24     <FilesMatch .php$>
25         SetHandler "proxy:fcgi://127.0.0.1:9000"
26     </FilesMatch>
27 
28     DocumentRoot "/yourpath"                    
29     <Directory "/yourpath">
30         Options FollowSymLinks
31         AllowOverride All
32         Require all granted
33     </Directory>     
34     # 开启反向代理ssl模块
35     SSLProxyEngine on
36     SSLProxyCheckPeerCN Off
37     SSLProxyCheckPeerName Off
38     Proxyrequests off
39     # 以下为配置重定向到Python中
40     RewriteEngine on  # 开启rewrite功能
41     RewriteCond %{REQUEST_URI} ^/[0-9]{1,4} #根据请求的url正则匹配1-4位字符
42     RewriteCond %{QUERY_STRING} ^c=(user&a=Add*) #根据?后的参数进行匹配
43     RewriteRule ^(.*) https://127.0.0.1:9052/1234/ [P]   #[p] 为反向代理
44 </VirtualHost>

nginx

实现技术:使用了nginx的$request_uri 进行正则匹配

 1 location ~ .php {
 2             root   htdocs;
 3             fastcgi_pass   127.0.0.1:9000;
 4             fastcgi_index  index.php;
 5             include        fastcgi_params;
 6             set $real_script_name $fastcgi_script_name;
 7             if ($fastcgi_script_name ~ "^(.+?.php)(/.+)$") {
 8                 set $real_script_name $1;
 9                 set $path_info $2;
10             }
11             # 以下为重定向到Python中
12             if ($request_uri ~* /d+/?c=user&a=Add){ #进行正则匹配
13                 proxy_pass https://127.0.0.1:9001/$request_uri; #反向代理到Python中
14                 break;
15             }
16             fastcgi_param SCRIPT_FILENAME $document_root$real_script_name;
17             fastcgi_param SCRIPT_NAME $real_script_name;
18 
19             fastcgi_param PATH_INFO $path_info;
20         } 
21     }

lighttpd

实现技术:通过lighttpd服务器反向代理技术,对整体请求进行二次识别,对url识别后,使用函数对参数进行识别,从而根据不同的url以及参数的变量进入到Python中,从而实现PHP+Python

 1 $SERVER["socket"] == "0.0.0.0:443" { 
 2     ssl.engine = "enable"
 3     ssl.pemfile = "/yourpath"
 4     ssl.ca-file = "/yourpath"
 5     server.document-root = "/yourpath" 
 6     $HTTP["url"] =~ "/" {
 7         
 8         fastcgi.server = ( 
 9             ".php" => (( 
10             "host" => "127.0.0.1", 
11             "port" => "9000" 
12                         ))),  
13 
14                 
15         $HTTP["querystring"] =~ "^/?c=user&a=Add"{
16             proxy.server = (
17                 "" => ((
18                     "host" => "127.0.0.1",
19                     "port" => 9088
20                 )))
21             }
22         }
23     url.rewrite-once = (
24         "^/(d*)/(.*)$" => "/$2&authid=$1",
25     )
26 }
27 
28 # python 虚拟主机
29 $SERVER["socket"] == "0.0.0.0:9088"{
30     # lighttpd官方文档提示,所被代理的服务器不支持SSL
31         # ssl.engine = "enable"
32         # ssl.pemfile = "/yourpath"
33         # ssl.ca-file = "/yourpath"
34         server.document-root = "/yourpath"
35         $HTTP["url"] !~ "^/static" {
36             fastcgi.server = ("/" => 
37                 ((
38                     "socket" => "/tmp/myapp-fcgi.sock",
39                     "bin-path" => "/yourpath",  
40                     "check-local" => "disable",
41                     "max-procs" => 1
42                 ))
43         )
44         # alias.url=("/"=>"/")
45         # here Solve the problem of URL redundant parameters
46          url.rewrite-once  = (
47         "^/(.*)&authid=(d*)$"=>"/runapp.fcgi/$2/$1",   # 这里将url重写进行写回
48          )
49     }
50 }

lighttpd官方文档解释如下:

 

原文地址:https://www.cnblogs.com/jmtang/p/12665050.html