1. 下载软件包
1.1 下载Nginx
wget http://nginx.org/download/nginx-1.11.4.tar.gz
tar zxvf nginx-1.11.4.tar.gz
1.2 下载缓存清除 模块 ngx_cache_purge
wget http://labs.frickle.com/files/ngx_cache_purge-2.3.tar.gz
tar zxvf ngx_cache_purge-2.3.tar.gz
1.3 下载Nginx Image 缩略图 模块 ngx_image_thumb-master
wget https://github.com/3078825/nginx-image/archive/master.zip
unzip master.zip
2. 安装环境依赖包
yum install autoconf automake zlib zlib-devel openssl openssl-devel pcre pcre-devel gcc gd-devel
3. 编译安装
3.1 配置
进入nginx解压目录,同时注意保持ngx_cache_purge与nginx为同级目录(安装模块时指定的目录路径)
./configure \
--prefix=/usr/local/nginx-1.11.4 \
--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/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--user=nginx \
--group=nginx \
--with-http_ssl_module \
--with-http_flv_module \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--http-client-body-temp-path=/var/tmp/nginx/client/ \
--http-proxy-temp-path=/var/tmp/nginx/proxy/ \
--http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ \
--add-module=../ngx_cache_purge-2.3 \
--add-module=../ngx_image_thumb-master
3.2 编译安装
make & make install
安装好之后,默认的nginx根目录为 /usr/local/nginx
4 配置
4.1 增加nginx用户
groupadd -f nginx
useradd -g nginx nginx
4.2 编写启动脚本
vi /etc/init.d/nginx
#!/bin/sh
#
# nginx - this script starts and stops the nginx daemon
#
# 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/sbin/nginx"
prog=$(basename $nginx)
NGINX_CONF_FILE="/etc/nginx/nginx.conf"
[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
lockfile=/var/lock/subsys/nginx
make_dirs() {
# make required directories
user=`nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
options=`$nginx -V 2>&1 | grep 'configure arguments:'`
for opt in $options; do
if [ `echo $opt | grep '.*-temp-path'` ]; then
value=`echo $opt | cut -d "=" -f 2`
if [ ! -d "$value" ]; then
# echo "creating" $value
mkdir -p $value && chown -R $user $value
fi
fi
done
}
start() {
[ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
make_dirs
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
sleep 1
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
4.3 添加脚本运行权限
chmod +x /etc/init.d/nginx
4.4 增加开机启动
chkconfig --add nginx
chkconfig nginx on
chkconfig nginx --list
5 配置nginx
5.1 nginx.conf
参考内容:
user www www;
worker_processes 1;
error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
pid logs/nginx.pid;
worker_rlimit_nofile 51200;
events {
use epoll;
worker_connections 51200;
}
http {
include 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 logs/access.log main;
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
server_name_in_redirect off;
client_max_body_size 10m;
client_body_buffer_size 128k;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
#keepalive_timeout 0;
keepalive_timeout 60;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.0;
gzip_comp_level 3;
gzip_disable "MSIE [1-6].";
gzip_types text/plain application/x-javascript text/css application/xml image/jpeg image/gif image/png; #添加图片压缩
gzip_vary on;
gzip_proxied any;
proxy_connect_timeout 300;
proxy_send_timeout 300;
proxy_read_timeout 300;
proxy_buffer_size 64k;
proxy_buffers 4 64k;
proxy_busy_buffers_size 128k;
proxy_temp_file_write_size 128k;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr; #传递真实ip给后端
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
open_file_cache max=204800 inactive=20s;
open_file_cache_min_uses 1;
open_file_cache_valid 30s;
proxy_cache_path /cache/proxy_cache levels=1:2 keys_zone=cache_one:100m inactive=1d max_size=30g; #100m和30G,按照服务要求,适当增大
proxy_temp_path /cache/proxy_temp;
include vhost/*.conf;
}
5.2 配置站点文件
upstream tomcat_server {
server 192.168.0.222 weight=1 max_fails=2 fail_timeout=30s; #添加ip,不是tomcat的8080端口
server 192.168.0.223 weight=1 max_fails=2 fail_timeout=30s;
}
server {
listen 80;
server_name 192.168.0.211;
index index.html index.jsp index.php;
location / {
proxy_next_upstream http_503 http_500 http_502 error timeout invalid_header;
proxy_cache cache_one;
add_header Nginx-Cache "$upstream_cache_status";
proxy_cache_key $host$uri$is_args$args;
proxy_set_header Accept-Encoding "";
proxy_pass http://tomcat_server;
proxy_cache_valid 200 304 12h;
proxy_cache_valid 301 302 1m;
proxy_cache_valid any 1m;
expires 1d;
}
#jsp,do文件不进行cache
location ~ .*\.(jsp|do)$ {
proxy_set_header Accept-Encoding ""; #只添加了一个, 其他的都添加到主配置文件了,以后添加站点不用在重复写
proxy_pass http://tomcat_server;
}
location ~ /purge(/.*) {
allow 127.0.0.1;
allow 192.168.0.0/24;
deny all;
proxy_cache_purge cache_one $host$1$is_args$args;
}
location /ngx_status
{
stub_status on;
access_log off;
allow 127.0.0.1;
allow 192.168.0.0/24; #自己的ip地址
deny all;
}
}
参考:
http://blog.youkuaiyun.com/kntao/article/details/46790027
http://www.cnblogs.com/kreo/p/4378086.html
http://www.open-open.com/lib/view/open1431254539701.html