Created
April 25, 2014 07:46
-
-
Save catalint/11281122 to your computer and use it in GitHub Desktop.
Putting nginx in front of the Apache server that comes with Cpanel can be tricky. Bellow is a script that reads the cpanel config files of the accounts and builds nginx server's (similar to Apache vhost) from them. It can also be added to do this automatically when new accounts and domains are registered on the server. The script's coding style …
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$conf='/etc/nginx/vhosts/vhost.conf'; | |
$confFile=fopen($conf,'w'); | |
exec("find /var/cpanel/userdata/* -type f -not -name '*cache*' -not -name '*db' -not -name 'main' -not -name 'vhost*'",$ary); | |
foreach ($ary as $f){ | |
$fis=file_get_contents($f); | |
preg_match("@\ndocumentroot..(.*)@",$fis,$ROOT); | |
$ROOT=$ROOT[1]; | |
preg_match("@\nip..(.*)@",$fis,$IP); | |
$IP=$IP[1]; | |
if(!strlen($IP)){ | |
echo "Err,nu am gasit ip la {$f}"; | |
exit; | |
} | |
preg_match("@\nport..(.*)@",$fis,$PORT); | |
$PORT=$PORT[1]; | |
// Change default ports to the one proxied | |
if(!$PORT)$PORT="81"; | |
if($PORT=="80")$PORT="81"; | |
if($PORT=="443")$PORT="82"; | |
preg_match("@\nservername..(.*)@",$fis,$SERVER_NAME); | |
$SERVER_NAME=$SERVER_NAME[1]; | |
preg_match("@\nserveralias..(.*)@",$fis,$SERVER_ALIAS); | |
$SERVER_ALIAS=$SERVER_ALIAS[1]; | |
$DOMAIN=$SERVER_NAME." ".$SERVER_ALIAS; | |
preg_match("@\nsslcertificatefile..(.*)@",$fis,$TMPCERT); | |
$TMPCERT=$TMPCERT[1]; | |
preg_match("@\nsslcertificatekeyfile..(.*)@",$fis,$TMP_KEY); | |
$TMP_KEY=$TMP_KEY[1]; | |
preg_match("@\nsslcacertificatefile..(.*)@",$fis,$TMPCERT_CA); | |
$TMPCERT_CA=$TMPCERT_CA[1]; | |
$isSSL=false; | |
$protocol='http'; | |
if(strlen($TMPCERT) && file_exists($TMPCERT)){ | |
$isSSL=true; | |
$protocol='https'; | |
} | |
$disableNGINX=false; | |
$obFlush=false; | |
$cacheJS_CSS=true; | |
if(stristr($DOMAIN,'www.cityinsurance.ro')){ // proxy some domains directly to Apache | |
$obFlush=true; | |
$cacheJS_CSS=false; | |
$disableNGINX=true; | |
} | |
if($isSSL){ | |
echo "Converting HTTPS for $DOMAIN\n"; | |
} | |
else{ | |
echo "Converting HTTP for $DOMAIN\n"; | |
} | |
$tpl=" | |
server { | |
error_log /var/log/nginx/vhost-error_log warn; | |
access_log /usr/local/apache/domlogs/$SERVER_NAME-bytes_log bytes_log; | |
"; | |
if($isSSL){ | |
$tpl.=" listen $IP:443 ssl;\n"; | |
} | |
else{ | |
$tpl.=" listen $IP:80;\n"; | |
} | |
if($isSSL){ | |
if(strlen($TMPCERT_CA)){ | |
$newFile=$TMPCERT.'.nginx.bundle.crt'; | |
file_put_contents($newFile,file_get_contents($TMPCERT)."\n".file_get_contents($TMPCERT_CA)); | |
$tpl.=" ssl_certificate {$newFile};\n ssl_certificate_key {$TMP_KEY};\n"; | |
} | |
else{ | |
$tpl.=" ssl_certificate {$TMPCERT};\n ssl_certificate_key {$TMP_KEY};\n"; | |
} | |
} | |
$tpl.=" server_name $DOMAIN;"; | |
$tpl.="\n location @proxy {\n"; | |
$tpl.=" proxy_pass {$protocol}://$IP:$PORT;\n"; | |
$tpl.=" | |
proxy_set_header Host \$host; | |
proxy_set_header X-Real-IP \$remote_addr; | |
proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for; | |
add_header X-proxy yes; | |
add_header X-apache yes; | |
}"; | |
if($cacheJS_CSS){ | |
$tpl.=" | |
location ~ nomin.*\.js(|\?ver.*)$ { | |
root $ROOT; | |
try_files \$uri /core/templates/default\$uri @proxy; | |
} | |
location ~ \.js(|\?ver.*)$ { | |
root $ROOT; | |
try_files \$uri /core/templates/default\$uri @proxy; | |
expires 192h; | |
add_header X-Static-File yes_minifed; | |
perl Minify::handler; | |
} | |
location ~* (/img/medium/|/img/large).*\.(gif|svg|jpg|jpeg|png|wmv|avi|mpg|mpeg|mp4|css|woff|txt|xls|doc|ico)(|\?ver.*)$ { | |
expires 192h; | |
try_files /invali_url_must_not_exist/ @proxy; | |
add_header X-Static-File no; | |
add_header X-bypass yes; | |
} | |
location ~* \.(gif|svg|jpg|jpeg|png|wmv|avi|mpg|mpeg|mp4|css|woff|txt|xls|doc|ico)(|\?ver.*)$ { | |
root $ROOT; | |
try_files \$uri /core/templates/default\$uri @proxy; | |
expires 192h; | |
add_header X-Static-File yes; | |
}"; | |
} | |
elseif(!$disableNGINX){ | |
$tpl.=" | |
location ~* \.(css|js)(|\?ver.*)$ { | |
root $ROOT; | |
try_files \$uri /core/templates/default\$uri @proxy; | |
add_header X-Static-File yes; | |
add_header X-Cache noCache; | |
} | |
location ~* \.(gif|svg|jpg|jpeg|png|wmv|avi|mpg|mpeg|mp4|css|woff|txt|xls|doc|ico)(|\?ver.*)$ { | |
root $ROOT; | |
try_files \$uri /core/templates/default\$uri @proxy; | |
add_header X-Static-File yes; | |
add_header X-Cache noCache; | |
} | |
"; | |
} | |
$tpl.=" | |
location / { | |
client_max_body_size 500M; | |
client_body_buffer_size 128k; "; | |
if($obFlush){ | |
$tpl.="gzip off;\n"; | |
$tpl.="proxy_buffering off;\n"; | |
$tpl.="add_header X-ob_flush yes;\n"; | |
} | |
$tpl.=" | |
proxy_connect_timeout 59s; | |
proxy_send_timeout 15m; | |
proxy_read_timeout 15m; | |
proxy_buffer_size 4k; | |
# you can increase proxy_buffers here to suppress an upstream response | |
# is buffered to a temporary file warning | |
proxy_buffers 16 32k; | |
proxy_busy_buffers_size 64k; | |
proxy_temp_file_write_size 64k; | |
add_header X-apache yes; | |
"; | |
foreach (explode(' ',$DOMAIN) as $d){ | |
$tpl.=" proxy_redirect {$protocol}://{$d}:$PORT {$protocol}://{$d};\n"; | |
} | |
$tpl.=" | |
proxy_pass {$protocol}://$IP:$PORT; | |
proxy_set_header Host \$host; | |
proxy_set_header X-Real-IP \$remote_addr; | |
proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for; | |
} | |
} | |
"; | |
fwrite($confFile,$tpl); | |
// echo $tpl; | |
} | |
fclose($confFile); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Putem discuta un pic pe skype? as avea cateva intrebari in legatura cu scriptul asta ... Imi poti da add pe: bms8197