您的位置:首页 > 运维架构 > Nginx

nginx根据域名动态代理

2015-10-28 00:00 591 查看
多个域名,存在数据库里面,访问到nginx的时候,根据不同域名,反向代理到不同的服务器上
编译nginx的时候要启用 auth_request --with-http_auth_request_module

nginx配置

http {
resolver 114.114.114.114;  # api返回的地址是域名,提示无法解析时添加该行
auth_request /api.asp;
auth_request_set $url $sent_http_url;

server {
listen       80;
server_name  localhost;

location / {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass $url;

}

location ~ \.asp$ {
root           html;
fastcgi_pass   127.0.0.1:9000;
fastcgi_index  index.php;
fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
include        fastcgi_params;
}
}

}

这里面把asp后缀解析为PHP,会提示Access to the script ‘asp‘ has been denied,把php-fpm 配置文件里面的security.limit_extensions 扩展加上asp 重启服务即可

api.asp 内容

<?php
ob_start();
$domain = substr($_SERVER['HTTP_HOST'],5);
$conn = mysql_connect("127.0.0.1","root","password");
if (!$conn)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("vpopmail", $conn);

$result = mysql_query("select host from tables where domain='".$domain."' limit 1");

while($row=mysql_fetch_array($result))
{
$url = "http://".$row['host'];
}

mysql_close($conn);

header("url: $url");
exit();
?>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: