nginx 1.8 Stable version:
wget http://nginx.org/download/nginx-1.8.0.tar.gz
ngx_cache_purge:
wget http://labs.frickle.com/files/ngx_cache_purge-2.3.tar.
Basic configuration of gz operating system:
1. Create nginx user
# useradd nginx
Set user password:
# passwd nginx
Modify the number of operating system processes and openfiles limits:
# vi /etc/security/limits.conf
Add the following 4 lines:
# add by nginx nginx hard nofile 65535 nginx soft nofile 65535 nginx hard nproc 4096 nginx soft nproc 4096
Save and exit.
# vi /etc/pam.d/login
add 1 line:
session required pam_limits.so
Save and exit.
Create a working directory for compilation:
# mkdir nginx-src
# mv nginx-1.8.0.tar.gz nginx-src/
# mv ngx_cache_purge-2.3.tar.gz nginx-src/
Unzip:
# cd nginx-src
# tar xzvf nginx -1.8.0.tar.gz
# tar xzvf ngx_cache_purge-2.3.tar.gz
Compile:
# cd nginx-1.8.0
# ./configure --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_stub_status_module --with-http_auth_request_module --with-mail --with-mail_ssl_module --with-file-aio --with-ipv6 --with-cc-opt='-O2 -g -pipe -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic'--add-module = .. / ngx_cache_purge-2.3
# make && make install
Create the cache path:
# mkdir –p /var/nginx_cache
Modify the configuration file:
# vi /etc/nginx/conf.d/default.conf
# Reverse proxy cache configuration proxy_cache_path /var/nginx_cache levels=2:2 keys_zone=websitecache:1024m inactive=1d max_size=10g; #Reverse proxy server URL configuration server { listen 80; server_name localhost; location / { proxy_pass http://10.12.2.57; proxy_set_header X-Real-IP $remote_addr; proxy_cache websitecache; proxy_cache_key $host$uri$is_args$args; proxy_ignore_headers Cache-Control Set-Cookie X-Accel-Expires Expires; proxy_hide_header Cache-Control; proxy_hide_header Set-Cookie; add_header X-Cache $upstream_cache_status; proxy_cache_valid 200 302 25m; proxy_cache_valid any 1m; } # Implement only proxy, no cache location ^~ /HQ/ { proxy_pass http://10.12.2.57; proxy_set_header X-Real-IP $remote_addr; } # Only proxy not cache location ^~ /flash/ { proxy_pass http://10.12.2.57; proxy_set_header X-Real-IP $remote_addr; } #purge module to clear the specified URL cache location ~ /clear(/.*) { allow 127.0.0.1; deny all; proxy_cache_purge websitecache $host$1$is_args$args; } }
Modify the main configuration file:
# vi /etc/nginx/nginx.conf
user nginx; worker_processes auto; error_log /var/log/nginx/error.log warn; pid /var/run/nginx.pid; events { worker_connections 2048; } 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"'; log_format format1 '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for" ' '$request_time' ; access_log /var/log/nginx/access.log main; sendfile on; #tcp_nopush on; keepalive_timeout 65; # Enable GZIP compression gzip on; gzip_static on; gzip_http_version 1.0; gzip_disable "MSIE [1-6]."; gzip_min_length 1020; gzip_comp_level 5; gzip_proxied any; gzip_types text/html text/plain text/css application/x-javascript application/javascript application/xml; include /etc/nginx/conf.d/*.conf; }
Save and exit after modification.
Create Linux Service
# vi /etc/init.d/nginx
#!/bin/sh # # nginx Startup script for nginx # # chkconfig: - 85 15 # processname: nginx # config: /etc/nginx/nginx.conf # config: /etc/sysconfig/nginx # pidfile: /var/run/nginx.pid # description: nginx is an HTTP and reverse proxy server # ### BEGIN INIT INFO # Provides: nginx # Required-Start: $local_fs $remote_fs $network # Required-Stop: $local_fs $remote_fs $network # Default-Start: 2 3 4 5 # Default-Stop: 0 1 6 # Short-Description: start and stop nginx ### END INIT INFO # Source function library. . /etc/rc.d/init.d/functions if [ -L $0 ]; then initscript=`/bin/readlink -f $0` else initscript=$0 be sysconfig=`/bin/basename $initscript` if [ -f /etc/sysconfig/$sysconfig ]; then . /etc/sysconfig/$sysconfig be nginx=${NGINX-/usr/sbin/nginx} prog=`/bin/basename $nginx` conffile=${CONFFILE-/etc/nginx/nginx.conf} lockfile=${LOCKFILE-/var/lock/subsys/nginx} pidfile=${PIDFILE-/var/run/nginx.pid} SLEEPMSEC=${SLEEPMSEC-200000} UPGRADEWAITLOOPS=${UPGRADEWAITLOOPS-5} RETVAL=0 start() { echo -n $"Starting $prog: " daemon --pidfile=${pidfile} ${nginx} -c ${conffile} RETVAL=$? echo [ $RETVAL = 0 ] && touch ${lockfile} return $RETVAL } stop() { echo -n $"Stopping $prog: " killproc -p ${pidfile} ${prog} RETVAL=$? echo [ $RETVAL = 0 ] && rm -f ${lockfile} ${pidfile} } reload() { echo -n $"Reloading $prog: " killproc -p ${pidfile} ${prog} -HUP RETVAL=$? echo } upgrade() { oldbinpidfile=${pidfile}.oldbin configtest -q || return echo -n $"Starting new master $prog: " killproc -p ${pidfile} ${prog} -USR2 echo for i in `/usr/bin/seq $UPGRADEWAITLOOPS`; do /bin/usleep $SLEEPMSEC if [ -f ${oldbinpidfile} -a -f ${pidfile} ]; then echo -n $"Graceful shutdown of old $prog: " killproc -p ${oldbinpidfile} ${prog} -QUIT RETVAL=$? echo return be done echo $"Upgrade failed!" RETVAL=1 } configtest() { if [ "$#" -ne 0 ] ; then case "$1" in -q) FLAG=$1 ;; *) ;; esac shift be ${nginx} -t -c ${conffile} $FLAG RETVAL=$? return $RETVAL } rh_status() { status -p ${pidfile} ${nginx} } # See how we were called. case "$1" in start) rh_status >/dev/null 2>&1 && exit 0 start ;; stop) stop ;; status) rh_status RETVAL=$? ;; restart) configtest -q || exit $RETVAL stop start ;; upgrade) rh_status >/dev/null 2>&1 || exit 0 upgrade ;; condrestart|try-restart) if rh_status >/dev/null 2>&1; then stop start be ;; force-reload|reload) reload ;; configtest) configtest ;; *) echo $"Usage: $prog {start|stop|restart|condrestart|try-restart|force-reload|upgrade|reload|status|help|configtest}" RETVAL=2 esac exit $RETVAL
Save and exit.
Register nginx service:
#chmod +x /etc/init.d/nginx
#chkconfig --add nginx
#chkconfig --level 2345 nginx on
#chkconfig --list nginx
Check the configuration:
# service nginx configtest
After confirming that there is no problem, start the service :
# service nginx start
If the ningx configuration is modified, it needs to take effect:
# service nginx reload