一、nginx的安装
本地环境:centos-x86 6.5 nginx-1.10.2
1.安装pcre库
yum install –y make zlib-devel openssl-devel pcre-devel
ubuntu系统:
apt-get update
apt-get install libpcre3 libpcre3-dev
apt-get install zlib1g-dev
apt-get install openssl
2.安装GCC 和GCC-C++
yum install gcc
yum install -y gcc gcc-c++
ubuntu系统:
apt-get install build-essential
apt-get install libtool
3.安装ngx_cache_purge-2.3
官网:http://labs.frickle.com/nginx_ngx_cache_purge/
文件名:ngx_cache_purge-2.3.tar.gz tar zxvf ngx_cache_purge-2.3.tar.gz
将解压文件夹放到nginx解压文件的同级目录
3.安装nginx(prefix指定nginx文件安装路径,with指定安装的插件
ngx_cache_purge模块的作用:用于清除指定url的缓存)
tar zxvf nginx-1.10.2.tar.gz
cd nginx-1.10.2
./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_gzip_static_module --with-http_stub_status_module --add-module=../ngx_cache_purge-2.3
make && make install
4.编写启动脚本
[root@localhost ~]# vi /etc/init.d/nginx
#!/bin/bash
# chkconfig: 345 99 20
# description: Nginx servicecontrol script
PROG="/usr/local/nginx/sbin/nginx"
PIDF="/usr/local/nginx/logs/nginx.pid"
case "$1" in
start)
$PROG
echo "Nginx servicestart success."
;;
stop)
kill -s QUIT $(cat $PIDF)
echo "Nginx service stopsuccess."
;;
restart)
$0 stop
$0 start
;;
reload)
kill -s HUP $(cat $PIDF)
echo"reload Nginx configsuccess."
;;
*)
echo "Usage: $0{start|stop|restart|reload}"
exit 1
esac
5.启动
[root@localhost ~]# chmod +x /etc/init.d/nginx
[root@localhost ~]# service nginx restart
[root@localhost ~]# chkconfig --add nginx
[root@localhost ~]# chkconfig nginx on
二、修改配置文件
worker_processes 4;
#error_log logs/error.log info;
events {
#使用epoll模型提高性能
use epoll;
#单个进程连接数(最大连接数=连接数*进程数)
worker_connections 65535;
}
http {
#文件扩展名与文件类型映射表
include mime.types;
#默认文件类型
default_type application/octet-stream;
#开启高效文件传输模式,sendfile指令指定nginx是否调用sendfile函数来输出文件,对于普通应用设为 on,
#如果用来进行下载等应用磁盘IO重负载应用,可设置为off,以平衡磁盘与网络I/O处理速度,降低系统的负载。
#注意:如果图片显示不正常把这个改成off
sendfile on;
#长连接超时时间,单位是秒
keepalive_timeout 65;
#gzip 是告诉nginx采用gzip压缩的形式发送数据。这将会减少我们发送的数据量。
gzip on;
gzip_min_length 1k; #最小1K
gzip_buffers 16 64K;
gzip_http_version 1.1;
gzip_comp_level 6;
gzip_types text/plain application/x- text/css application/xml application/;
gzip_vary on;
#header设置:用户真实的ip地址转发给后端服务器
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
#缓冲
client_body_buffer_size 512k;
proxy_connect_timeout 5;
proxy_read_timeout 60;
proxy_send_timeout 5;
proxy_buffer_size 16k;
proxy_buffers 4 64k;
proxy_busy_buffers_size 128k;
proxy_temp_file_write_size 128k;
##########################缓存#####################
#设置缓存临时目录,要配合linux的内存目录/dev/shm使用的话,必须给赋予目录权限,因为默认root权限
proxy_temp_path /mydata/proxy_temp_path;
#设置缓存目录,并设置Web缓存区名称为cache_one,内存缓存空间大小为256m,7天没有被访问的内容自动清除,硬盘缓存空间大小为40GB。
proxy_cache_path /mydata/proxy_cache_path levels=1:2 keys_zone=cache_one:256m inactive=7d max_size=80g;
#启用html、jsp...标签不缓存的设置
proxy_ignore_headers X-Accel-Expires Expires Cache-Control Set-Cookie;
################################################集群 ###################################################
#动态资源集群
upstream dynamic {
ip_hash;#解决跨域问题
server 127.0.0.1:8080 weight=1;
}
#静态资源集群(一般由nginx管理,因为nginx处理静态资源性能好,如果服
#务器有限,也可以部署在代理服务器本地)
upstream static {
ip_hash;#解决跨域问题
server 127.0.0.1:8081 weight=1;
}
#后台资源集群
upstream admin {
ip_hash;#解决跨域问题
server 127.0.0.1:9070 weight=1;
}
################################################Nginx web代理###################################################
server {
listen 80;
proxy_intercept_errors on;
server_name web;
location / {
proxy_pass http://dynamic;
}
#用于清除缓存的url设置
#假设一个URL为demo/test.gif,那么就可以通过访问/purge/demo/test.gif清除该URL的缓存。
location ~ /purge(/.*) {
#设置只允许指定的IP或IP段才可以清除URL缓存
allow 127.0.0.1;
allow 183.233.212.166;
deny all;
proxy_cache_purge cache_one $host$1$is_args$args;
}
location ~ .*\.(js|css|ico|jpg|jpeg|png|JPG|JPEG|PNG|eot|svg|ttf|woff) {
proxy_pass http://static;
}
location ~ .*$ {
proxy_pass http://dynamic;
}
location = /error.html {
# 放错误页面的目录路径。
root /mydata/program/tomcat-7.0/webapps/ROOT/error;
}
}
################################################Nginx admin代理(获取后台房源图片)###################################################
server {
listen 9080;
server_name admin;
#用于清除缓存的url设置
#假设一个URL为demo/test.gif,那么就可以通过访问/purge/demo/test.gif清除该URL的缓存。
location ~ /purge(/.*) {
#设置只允许指定的IP或IP段才可以清除URL缓存
allow 127.0.0.1;
allow 183.233.212.166;
deny all;
proxy_cache_purge cache_one $host$1$is_args$args;
}
location ~ /upload(/.*)\.(ico|jpg|jpeg|png|JPG|JPEG|PNG) {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Allow-Methods' '*';
proxy_cache cache_one; #设置资源缓存的zone
proxy_cache_key $host$uri$is_args$args; #设置缓存的key,以域名、URI、参数组合成Web缓存的Key值,Nginx根据Key值哈希,存储缓存内容到二级缓存目录内
proxy_cache_valid 200 304 12h; #对不同的HTTP状态码设置不同的缓存时间
proxy_ignore_headers X-Accel-Expires Expires Cache-Control Set-Cookie;
proxy_hide_header Cache-Control;
proxy_hide_header Set-Cookie;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
#增加一个header字段方便在浏览器查看是否击中缓存(生产中可注释)
add_header X-Cache '$upstream_cache_status from $host';
root /mydata/mz_images/;
expires 15d; #缓存15天
}
location ~ /ueditor(/.*)\.(ico|jpg|jpeg|png|JPG|JPEG|PNG) {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Allow-Methods' '*';
proxy_cache cache_one; #设置资源缓存的zone
proxy_cache_key $host$uri$is_args$args; #设置缓存的key,以域名、URI、参数组合成Web缓存的Key值,Nginx根据Key值哈希,存储缓存内容到二级缓存目录内
proxy_cache_valid 200 304 12h; #对不同的HTTP状态码设置不同的缓存时间
proxy_ignore_headers X-Accel-Expires Expires Cache-Control Set-Cookie;
proxy_hide_header Cache-Control;
proxy_hide_header Set-Cookie;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
#增加一个header字段方便在浏览器查看是否击中缓存(生产中可注释)
add_header X-Cache '$upstream_cache_status from $host';
root /mydata/mz_images/;
expires 15d; #缓存15天
}
location ~ .*$ {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Allow-Methods' '*';
proxy_pass http://admin;
client_max_body_size 10m;
}
}
#静态资源服务器,这里监听本地8081端口,因为静态资源服务器和代理服务器是同一台机器,所以有如下配置
#如果是独立的服务器,直接在集群upstram配置即可。
server{
listen 8081;
server_name static;
location ~ .*\.(js|css|ico|jpg|jpeg|png|JPG|JPEG|PNG|eot|svg|ttf|woff) {
proxy_cache cache_one; #设置资源缓存的zone
proxy_cache_key $host$uri$is_args$args; #设置缓存的key,以域名、URI、参数组合成Web缓存的Key值,Nginx根据Key值哈希,存储缓存内容到二级缓存目录内
proxy_cache_valid 200 304 12h; #对不同的HTTP状态码设置不同的缓存时间
proxy_ignore_headers X-Accel-Expires Expires Cache-Control Set-Cookie;
proxy_hide_header Cache-Control;
proxy_hide_header Set-Cookie;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
#增加一个header字段方便在浏览器查看是否击中缓存(生产中可注释)
add_header X-Cache '$upstream_cache_status from $host';
#所有静态文件直接读取硬盘
#root /app/tomcat7/webapps/ROOT;
proxy_pass http://dynamic;
expires 15d; #缓存15天
}
}
}