Home > IT Architecture, Linux, Systems > Install nginx reverse proxy server under centos(bash shell)

Install nginx reverse proxy server under centos(bash shell)

September 13th, 2010

This script by me accomplish the auto installation of nginx reverse server under centos.Please modify upstream and server_name scope in nginx.conf after the installation.And you do not need to modify proxy.conf(Of course,you can.For example,to get the real user ip in backend apache server).

Here the script goes:
#!/bin/bash
#nginx_reverse_proxy_centos.sh
#by doxer.org
os="";
grep "centos" /etc/issue -i -q
if [ $? = '0' ];then
os='centos'
fi

grep "debian" /etc/issue -i -q
if [ $? = '0' ];then
os='debian'
fi

if [ $os = "" ];then
echo "not a valid system os"
exit 1
fi
#set variables begin
downloads="/root/downloads"

#set variables end

#centos begin
rpm -qa|grep httpd #under centos it's httpd,debian apache
if [ $? = '0' ];then
aa="`rpm -qa|grep httpd`"
rpm -e $aa
fi

yum -y install gcc gcc-c++ zlib-devel openssl-devel #expect Is this ok =>y
if [ ! -d $downloads ];then
mkdir $downloads
fi
cd $downloads #pwd /root/downloads
#install pcre begin
wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-7.9.tar.gz
tar zxvf pcre-7.9.tar.gz
cd pcre-7.9/ #pwd /root/downloads/pcre-7.9
./configure
make && make install
#install pcre end
cd .. #pwd /root/downloads
#install nginx begin
wget http://nginx.org/download/nginx-0.7.63.tar.gz
tar zxvf nginx-0.7.63.tar.gz
mkdir -p /usr/local/nginx
cp -R ./nginx-0.7.63/* /usr/local/nginx
cd /usr/local/nginx #pwd /usr/local/nginx
./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
make && make install
mkdir -p /usr/local/nginx/logs
chmod +w /usr/local/nginx/logs
groupadd www
useradd -g www www
chown -R www:www /usr/local/nginx/logs
touch  /usr/local/nginx/conf/proxy.conf
echo "proxy_redirect off;
proxy_set_header Host \$host;
proxy_set_header Port \$proxy_port;
proxy_set_header XHost \$host:\$proxy_port;
proxy_set_header X-Real-IP \$remote_addr;
proxy_set_header REMOTE_ADDR \$remote_addr;
proxy_set_header HTTP_CLIENT_IP \$remote_addr;
proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
#client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_connect_timeout 30;
proxy_send_timeout 30;
proxy_read_timeout 30;
proxy_buffers 32 4k;
proxy_buffer_size 16k;
expires 60;
client_body_timeout 60;
client_header_timeout 60;">/usr/local/nginx/conf/proxy.conf
rm -f /usr/local/nginx/conf/nginx.conf
touch /usr/local/nginx/conf/nginx.conf
echo -e 'user www www;
worker_processes 4;
error_log /usr/local/nginx/logs/error.log crit;
pid /usr/local/nginx/nginx.pid;

events {
use epoll;
worker_connections 1024;
}

http {
include mime.types;
include proxy.conf;
default_type application/octet-stream;
charset utf-8;
sendfile on;
tcp_nopush on;
keepalive_timeout 30;
gzip on;
gzip_min_length 1k;
#gzip_buffers 4 1
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml;
gzip_vary on;

upstream localhost{
server 127.0.0.1:81;
}

server{
listen 80;
server_name _;
location / {
proxy_pass http://localhost;
}
}

server {
listen 80;
server_name _;
location / {
if ($host !~* www\.(.*)) {
set $host_without_www $1;
set $xhost www.$host;
rewrite ^(.*)$ http://$xhost$host_without_www$1 permanent;
}
proxy_pass http://localhost;
}

location /ns {
stub_status on;
}
location ~ /\.ht {
deny all;
}
}
}'>/usr/local/nginx/conf/nginx.conf
#install nginx end

/usr/local/nginx/sbin/nginx #start nginx
ps aux|grep nginx|grep grep #check if nginx running now
if [ $? = '0' ];then
echo "nginx is running now"
else
echo "not running"
fi
#centos end
Ok.That's all.Now start up the nginx server:/usr/local/nginx/sbin/nginx.Change the listening port on apache to 81(NameVirtualHost *:81 and <VirtualHost *:81>).Visit your site and now take a look at the response header sent by your nginx reverse server.You will find Server:nginx/0.7.30 in your firebug's Net label.Enjoy the high concurrent processing ability of nginx now!

Related:Install nginx reverse proxy server under debian(bash shell)

Good Luck!


  1. George
    December 25th, 2010 at 11:58 | #1

    Hi thanks for sharing this bash shell script as i am getting tired of manually doing this! I have edited your base script to suite my needs at http://forum.nginx.org/read.php?11,161010 – haven’t tested it yet. Wondering if you have ideas for bash shell scripts for full apache replacement using nginx ?

    Also what do you mean by bootstrap from start the nginx server ? Thanks again!

    • lamp-linux
      August 2nd, 2011 at 13:39 | #2

      Hi, just forget about bootstrap thing. I just wanted to express it’s to the step of starting up nginx server.
      Thanks.

Comments are closed.