Документация по ОС FreeBSD Воскресенье, 28.04.2024, 13:24
Приветствую Вас Гость | RSS
Меню сайта

Категории каталога
Apache [58]
DNS [25]
FTP [27]
Mail [74]
Samba [24]
Squid [46]
SSH [23]
VPN [35]
РРР [20]
Net [173]

Главная » Статьи » Сеть » Apache

Создание Виртуальных хостов nginx из конфига Apache [2008]

В последнее время всем интересна тема установки Nginx как front-end сервера и Apache как back-end сервера.
Компания admin-sys написала скрипт который парсит httpd.conf ,находит там винтуальные хосты и по этим данным строит виртуальные хосты в nginx.conf
1)check_httpdconf.sh

#!/usr/local/bin/bash

NGINX_PID=/var/run/nginx.pid
HTTPD_PID=/var/run/httpd.pid
NGINX=/usr/local/sbin/nginx

if [ ${HTTPD_PID} -nt ${NGINX_PID} ]; then
/root/nginx/apacheconfparse.pl > /root/nginx/nginx.conf_middle
/bin/cat /root/nginx/nginx.conf_top /root/nginx/nginx.conf_middle /root/nginx/nginx.conf_bottom > /usr/local/etc/nginx/nginx.conf
/usr/local/etc/rc.d/nginx restart
fi

2)apacheconfparse.pl

#!/usr/bin/perl

use Data::Dumper;

# script configuration variables
$debug = 0;
$initial_http_conf = "/etc/httpd/conf/httpd.conf";
%logfiles = ();
$logtype = "combined";
$bytes_field = 6;
$time_field = 7;
# ------------------------------

sub parseconffile {
my $filename = shift;
my $content = "";
$debug and print ". parseconffile: opening $filename \n";
open (FILE, $filename);
while () { next if (/^[ \t]*\#/); $content .= $_; }
close FILE;

@virtialhost_sections = ( $content =~ //sg);
$debug and print ". parseconffile: found ".scalar(@virtialhost_sections)." virtual host sections \n";

foreach $virtialhost_section (@virtialhost_sections) {
$debug and print ". . parseconffile: parsing virtual host section \n";
(my $vh_ips) = $virtialhost_section =~ /[ \t]*(.*?)>/;
$debug and print ". . parseconffile: VH on = ".$vh_ips."\n";
($vh_ip,$vh_port) = split(":",$vh_ips);
if (!$vh_port) { $vh_port = 8080; }
$debug and print ". . parseconffile: (ip = ".$vh_ip.", port = ".$vh_port.")\n";
(my $servername) = $virtialhost_section =~ /ServerName[ \t]*(.*?)[\r\n]/;
$debug and print ". . parseconffile: ServerName = ".$servername."\n";
(my $serveraliases_list) = $virtialhost_section =~ /ServerAlias[ \t]*(.*?)[\r\n]/;
$debug and print ". . parseconffile: ServerAliases List = ".$serveraliases_list."\n";
my @server_aliases = split (/[ \t]/, $serveraliases_list);
$debug and print ". . parseconffile: (".scalar(@server_aliases)." aliases)\n";
(my $documentroot) = $virtialhost_section =~ /DocumentRoot[ \t]*(.*?)[\r\n]/;
$debug and print ". . parseconffile: DocumentRoot = ".$documentroot."\n";
(my $logfilename) = ($virtialhost_section =~ /CustomLog (.*) $logtype/);
(my $byteslogfilename) = ($virtialhost_section =~ /CustomLog (.*) bytes/);
my $bytesconf = "";
if ($byteslogfilename) {
$bytesconf = "access_log $byteslogfilename bytes;"
}
if ($logfilename) {
$logfiles{$logfilename} = $servername;
$debug and print ". . parseconffile: CustomLog = ".$logfilename."\n";
}

$debug and print " =====================================================================================\n";
if ($vh_port == 8080 ) {
print ) {
next if (/^[ \t]*\#/);
chomp;
($debug == 2) and print ". . searchforincludes: checking string $_ \n";
if (/Include[ \t]*(.*)/) {
my @includefile = $_ =~ /Include[ \t]*(.*)$/;
$debug and print ". ! searchforincludes: Include found: ($_) ".$includefile[0]." \n";
push @includefiles, $includefile[0];
}
}
close FILE;
return @includefiles;
}

$debug and print ("Version 0.1\n");
my @includefiles = searchforincludes($initial_http_conf);

parseconffile ($initial_http_conf);
foreach $includefile (@includefiles) { parseconffile ($includefile); }
$debug and print "%logfiles hash dump:\n".Dumper(\%logfiles);

@includefiles = searchforincludes("/etc/httpd/conf/extra/directadmin-vhosts.conf");
foreach $includefile (@includefiles) { parseconffile ($includefile); }
$debug and print "%logfiles hash dump:\n".Dumper(\%logfiles);

#foreach $logfile (keys %logfiles) {
# $virtualhost = $logfiles{$logfile};
# $debug and print "Parsing logfile $logfile for VH $virtualhost\n";
# open (FILE, $logfile);
# while () {
# chomp;
# next if (/^[ \t]*\#/);
#
# #$content .= $_;
# }
# close FILE;
#}

3)nginx.conf_top

user apache apache;
worker_processes 2;

events {
worker_connections 1024;
}

http {
include /usr/local/etc/nginx/mime.types;
default_type application/octet-stream;

log_format main '$remote_addr - $remote_user [$time_local] '
'"$request" "$http_referer" $status $bytes_sent '
'"$http_user_agent" "http_x_forwarded_for"';

log_format bytes '$bytes_sent';

access_log /var/log/nginx/access.log main;

#worker_rlimit_nofile 80000;
#events {
# worker_connections 50000;
#}

reset_timedout_connection on;
client_header_timeout 3m;
client_body_timeout 3m;
send_timeout 3m;

client_header_buffer_size 1k;
large_client_header_buffers 4 4k;
server_names_hash_bucket_size 128;

gzip on;
gzip_min_length 1100;
gzip_buffers 4 8k;
gzip_types text/plain;

output_buffers 1 32k;
postpone_output 1460;

sendfile on;
tcp_nopush on;
tcp_nodelay on;

4)nginx.conf_bottom

#!!! ADD ADDITIONAL VIRTUAL HOSTS BEFORE THIS LINE!!!
}

5)nginx.conf_middle
До первого запуска пустой файл

6)Настройка
-Все фвйлы скрипты должны лежать в /root/nginx
-В скриптах проверке правильность путей к конфигам
-запус по cron
*/5 * * * * root /root/nginx/check_httpdconf.sh



Источник: http://dedic.ru/fatik
Категория: Apache | Добавил: oleg (02.06.2008)
Просмотров: 1661 | Рейтинг: 0.0/0 |
Всего комментариев: 0
Добавлять комментарии могут только зарегистрированные пользователи.
[ Регистрация | Вход ]
Форма входа

Beastie

Друзья сайта

Статистика

Онлайн всего: 1
Гостей: 1
Пользователей: 0
links

Copyright MyCorp © 2024