Created
January 17, 2015 16:50
-
-
Save logical-and/6c9b5a172026ffdbebdf to your computer and use it in GitHub Desktop.
virtualmin-apache-and-nginx-reverse-proxy.html commentary
This file contains 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
Nextgen version :) | |
## | |
# Template | |
## | |
server { | |
listen {SITE_IP}:80; | |
server_name {SITE_DOMAIN} www.{SITE_DOMAIN}; | |
root {HOME_DIR}/{PUBLIC_HTML_DIR}; | |
access_log {HOME_DIR}/logs/nginx_access_log; | |
error_log {HOME_DIR}/logs/nginx_error_log; | |
location @apache_handler { | |
proxy_pass http://{SITE_IP}:8080; | |
include /etc/nginx/proxy.conf; | |
} | |
location / { | |
try_files $uri @apache_handler; | |
} | |
location ~* ^.+\.(jpe?g|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mp3)$ { | |
expires max; | |
log_not_found off; | |
access_log off; | |
try_files $uri @apache_handler; | |
} | |
location ~* (favicon\.ico|robots.txt) { | |
log_not_found off; | |
access_log off; | |
} | |
} | |
## | |
# Hook | |
## | |
#!/bin/bash | |
NGINX_CONF_TEMPLATE="/etc/nginx/sites-available/template.conf" | |
NGINX_CONF_EXT="gen.conf" | |
NGINX_CONF_FILE="/etc/nginx/sites-available/${VIRTUALSERVER_DOM}.${NGINX_CONF_EXT}" | |
NGINX_CONF_SYMLINK="/etc/nginx/sites-enabled/${VIRTUALSERVER_DOM}.${NGINX_CONF_EXT}" | |
function create_from_template { | |
if [ ! -f $NGINX_CONF_FILE ]; then | |
cp $NGINX_CONF_TEMPLATE $NGINX_CONF_FILE | |
echo -e "Creating configuration from template ${NGINX_CONF_FILE}...\n\n" | |
fi | |
} | |
function make_replaces { | |
perl -pi -e "s#{SITE_DOMAIN}#$VIRTUALSERVER_DOM#g" $NGINX_CONF_FILE | |
perl -pi -e "s#{SITE_IP}#$VIRTUALSERVER_IP#g" $NGINX_CONF_FILE | |
perl -pi -e "s#{HOME_DIR}#$VIRTUALSERVER_HOME#g" $NGINX_CONF_FILE | |
perl -pi -e "s#{PUBLIC_HTML_DIR}#$VIRTUALSERVER_PUBLIC_HTML_DIR#g" $NGINX_CONF_FILE | |
} | |
# Create symlink if not exists | |
function create_symlink { | |
if [ ! -h $NGINX_CONF_SYMLINK ]; then | |
ln -s $NGINX_CONF_FILE $NGINX_CONF_SYMLINK | |
echo -e "Symlinking ${NGINX_CONF_SYMLINK}...\n" | |
fi | |
} | |
if [ "$VIRTUALSERVER_ACTION" = "CREATE_DOMAIN" ]; then | |
if [ "${VIRTUALSERVER_WEB}" = "1" ]; | |
then | |
create_from_template | |
make_replaces | |
create_symlink | |
/etc/init.d/nginx reload | |
fi | |
elif [ "$VIRTUALSERVER_ACTION" = "DELETE_DOMAIN" ]; then | |
if [ "${VIRTUALSERVER_WEB}" = "1" ]; | |
then | |
rm $NGINX_CONF_FILE | |
rm NGINX_CONF_FILE | |
/etc/init.d/nginx reload | |
fi | |
elif [ "$VIRTUALSERVER_ACTION" = "MODIFY_DOMAIN" ]; then | |
if [ "${VIRTUALSERVER_WEB}" = "1" ]; | |
then | |
if [ ! -f $NGINX_CONF_FILE ]; then | |
create_from_template | |
make_replaces | |
fi | |
fi | |
create_symlink | |
if [ "$VIRTUALSERVER_DOM" != "$VIRTUALSERVER_OLDSERVER_DOM" ]; then | |
if [ "${VIRTUALSERVER_WEB}" = "1" ]; | |
then | |
OLD_NGINX_CONF_FILE=/etc/nginx/sites-available/${VIRTUALSERVER_OLDSERVER_DOM}.${NGINX_CONF_EXT} | |
mv $OLD_NGINX_CONF_FILE $NGINX_CONF_FILE | |
rm /etc/nginx/sites-enabled/${VIRTUALSERVER_OLDSERVER_DOM}.${NGINX_CONF_EXT} | |
perl -pi -e "s#$VIRTUALSERVER_OLDSERVER_DOM#$VIRTUALSERVER_DOM#g" $NGINX_CONF_FILE | |
perl -pi -e "s#$VIRTUALSERVER_OLDSERVER_IP#$VIRTUALSERVER_IP#g" $NGINX_CONF_FILE | |
perl -pi -e "s#$VIRTUALSERVER_OLDSERVER_HOME#$VIRTUALSERVER_HOME#g" $NGINX_CONF_FILE | |
fi | |
fi | |
create_symlink | |
if [ "${VIRTUALSERVER_WEB}" = "1" ]; | |
then | |
/etc/init.d/nginx reload | |
fi | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment