Many webservers behind a single IP
- Quinta-feira Fev 11,2010 10:34 PM
- By admin
- In Sem categoria
The problem
- One public IP address
- Multiple webservers
The solutions
- Use different ports
- Your users will have to know the correct port or may end up on a wrong website
- Use a single webserver
- You may require multiple webservers due to web-apps needs)
- Use a webproxy
- The one I’m going to explain
How it works
Your webproxy will be on port 80 accepting all http connections, it’ll send the requests to the correct webserver
based on the hostname.
eg.
site1.com > 192.168.1.11
site2.com > 192.168.1.12
site3.com > 192.168.1.13
NGinx setup
Download nginx
# wget http://nginx.org/download/nginx-0.8.33.tar.gz
Uncompress nginx
# tar -zxf nginx-0.8.33.tar.gz
Enter on nginx directory
# cd nginx-0.8.33
Compile and install nginx
- Since it’s a proxy most of the modules aren’t needed i’ll just skip them.
- Openssl support is required for https
- Ipv6 is required on my setup and is more futureproof
# ./configure --without-select_module --without-poll_module --without-http_charset_module --without-http_gzip_module --without-http_ssi_module --without-http_userid_module --without-http_access_module --without-http_auth_basic_module --without-http_autoindex_module --without-http_geo_module --without-http_map_module --without-http_referer_module --without-http_rewrite_module --without-http_fastcgi_module --without-http_memcached_module --without-http_limit_zone_module --without-http_limit_req_module --without-http_empty_gif_module --without-http_browser_module --without-http_upstream_ip_hash_module --without-mail_pop3_module --without-mail_imap_module --without-mail_smtp_module --without-pcre --with-openssl=/usr/lib/openssl --with-ipv6 # make # make install
Configure nginx
# cd /etc/nginx/ # nano nginx.conf
Default nginx.conf’d work fine, just be sure you’ve the line:
include /etc/nginx/conf.d/*.conf;
It should look like this:
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log;
#error_log /var/log/nginx/error.log notice;
#error_log /var/log/nginx/error.log info;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] $request '
'"$status" $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
}
VirtualHosts
site1.com vhost / default
/etc/nginx/conf.d/site1.com
server {
# For ipv6 only or ipv6 + ipv4
listen [::]:80 default;
# For ipv4 only
# listen 80 default;
server_name www.site1.com;
location / {
proxy_pass http://192.168.1.11:80;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_redirect off;
}
}
/etc/nginx/conf.d/site2.com
server {
# For ipv6 only or ipv6 + ipv4
listen [::]:80;
# For ipv4 only
# listen 80;
server_name www.site2.com;
location / {
proxy_pass http://192.168.1.12:80;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_redirect off;
}
}
Init.d script
#!/bin/sh
#
# nginx - this script starts and stops the nginx daemin
#
# chkconfig: - 85 15
# description: Nginx is an HTTP(S) server, HTTP(S) reverse \
# proxy and IMAP/POP3 proxy server
# processname: nginx
# config: /etc/nginx/nginx.conf
# config: /etc/sysconfig/nginx
# pidfile: /var/run/nginx.pid
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0
nginx="/usr/local/nginx/sbin/nginx"
prog=$(basename $nginx)
NGINX_CONF_FILE="/etc/nginx/nginx.conf"
[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
lockfile=/var/lock/subsys/nginx
start() {
[ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
echo -n $"Starting $prog: "
daemon $nginx -c $NGINX_CONF_FILE
retval=$?
echo
[ $retval -eq 0 ] && touch $lockfile
return $retval
}
stop() {
echo -n $"Stopping $prog: "
killproc $prog -QUIT
retval=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
}
restart() {
configtest || return $?
stop
start
}
reload() {
configtest || return $?
echo -n $"Reloading $prog: "
killproc $nginx -HUP
RETVAL=$?
echo
}
force_reload() {
restart
}
configtest() {
$nginx -t -c $NGINX_CONF_FILE
}
rh_status() {
status $prog
}
rh_status_q() {
rh_status >/dev/null 2>&1
}
case "$1" in
start)
rh_status_q && exit 0
$1
;;
stop)
rh_status_q || exit 0
$1
;;
restart|configtest)
$1
;;
reload)
rh_status_q || exit 7
$1
;;
force-reload)
force_reload
;;
status)
rh_status
;;
condrestart|try-restart)
rh_status_q || exit 0
;;
*)
echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
exit 2
esac
Change it’s permissions
#chmod 755 /etc/init.d/nginx
Start nginx
#/etc/init.d/nginx start
Set nginx to start on boot
chkconfig nginx on
That’s it, now all you have to do is to forward port 80 on your router to nginx server and it’ll do the rest.
If you have any questions feel free to leave a comment.