(1)安装依赖软件pcre-devel和libevent
[root@bogon nginx]# yum install pcre-devel libevent openssl
(2)安装配置nginx
[root@bogon nginx]# wget http://nginx.org/download/nginx-1.6.0.tar.gz
[root@bogon nginx]# tar -zxvf nginx-1.6.0.tar.gz -C /usr/local/src/
[root@bogon nginx]# cd /usr/local/src/nginx-1.6.0/
[root@bogon nginx-1.6.0]# ./configure --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/ --with-pcre
出错error: SSL modules require the OpenSSL library.就执行 yum -y install openssl openssl-devel
[root@bogon nginx-1.6.0]# make && make install
(3)为了能够启动服务因此要把控制脚本放到搜索路径去,因此要编辑/etc/profile文件
[root@bogon nginx]# vim /etc/profile
##################
if [ -z "$INPUTRC" -a ! -f "$HOME/.inputrc" ]; then
INPUTRC=/etc/inputrc
fi
加上PATH=$PATH:/usr/local/nginx/sbin
export PATH USER LOGNAME MAIL HOSTNAME HISTSIZE INPUTRC
########################
[root@bogon nginx]# . /etc/profile
//创建用户和组
[root@bogon ~]# groupadd -r nginx
[root@bogon ~]# useradd -r -g nginx nginx
[root@bogon ~]# mkdir -pv /var/tmp/nginx/client
[root@bogon ~]# mkdir -pv /var/tmp/nginx/proxy
//测试有没有语法错误
[root@192 nginx-1.6.0]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
//启动nginx服务,并查看端口
[root@localhost nginx-1.6.0]# nginx
[root@localhost nginx-1.6.0]# netstat -tupln |grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 6888/nginx: master
[root@localhost nginx-1.6.0]#
访问:192.168.1.108:80如果存在欢迎页面则成功
(4)反向代理到百度
--备份
cp /etc/nginx/nginx.conf /etc/nginx/nginx_bak.conf
--编辑
vi /etc/nginx/nginx.conf
#################修改这段#################
location / {
proxy_pass http://www.baidu.com/;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
#############################################
重启
[root@192 nginx-1.6.0]# pkill -9 nginx
[root@192 nginx-1.6.0]# netstat -tupln | grep nginx
[root@192 nginx-1.6.0]# nginx
访问成功
(5).缓存和gzip
##创建缓存文件夹
mkdir /usr/local/nginx/proxy_cache
mkdir /usr/local/nginx/proxy_temp
nginx.conf配置
http {
#gzip模块设置
gzip on; #开启gzip压缩输出
gzip_min_length 1k; #最小压缩文件大小
gzip_buffers 4 16k; #压缩缓冲区
gzip_http_version 1.0; #压缩版本(默认1.1,前端如果是squid2.5请使用1.0)
gzip_comp_level 2; #压缩等级
gzip_types gzip_types text/plain application/x-javascript text/css text/javascript application/x-httpd-php image/jpeg image/gif image/png;
#压缩类型,默认就已经包含text/html,所以下面就不用再写了,写上去也不会有问题,但是会有一个warn。
gzip_vary on;
gzip_proxied any;
##IE6对Gzip不怎么友好,不给它Gzip了##
gzip_disable "MSIE [1-6]\.";
##cache缓存设置##
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;
##定义从后端服务器接收的临时文件的存放路径##
proxy_temp_path /usr/local/nginx/proxy_temp;
##设置缓存的路径和其他参数。##
proxy_cache_path /usr/local/nginx/proxy_cache levels=1:2 keys_zone=cache_one:200m inactive=1d max_size=30g;
##end##
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://192.168.1.24;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
##前端将Accept-Encoding传给后端服务器 两者之间传输压缩
proxy_set_header Accept-Encoding 'gzip'
}
#所有静态文件由nginx直接读取不经过tomcat
location ~ .*\.(gif|jpg|png|htm|html|css|js|flv|ico|swf)(.*) {
proxy_pass http://192.168.1.24;
proxy_redirect off;
proxy_set_header Host $host;
proxy_cache cache_one;
##为不同的响应状态码设置不同的缓存时间。##
proxy_cache_valid 200 302 1h;
proxy_cache_valid 301 1d;
proxy_cache_valid any 1m;
expires 30d;
}
}
curl -I -H "Accept-Encoding: gzip, deflate" "http://192.168.81.128/SpringmvcMybatis/js/jquery.min.js"
HTTP/1.1 200 OK
Server: nginx/1.6.0
Date: Mon, 02 Mar 2015 07:40:08 GMT
Content-Type: text/javascript
Connection: keep-alive
Vary: Accept-Encoding
Last-Modified: Mon, 02 Mar 2015 06:29:26 GMT
Expires: Wed, 01 Apr 2015 07:40:08 GMT
Cache-Control: max-age=2592000
Content-Encoding: gzip
小于1K的不进行gzip
[root@192 nginx]# curl -I -H "Accept-Encoding: gzip, deflate" "http://192.168.81.128/SpringmvcMybatis/js/jquery.min2.js"
HTTP/1.1 200 OK
Server: nginx/1.6.0
Date: Mon, 02 Mar 2015 07:46:46 GMT
Content-Type: text/javascript
Content-Length: 2
Connection: keep-alive
Last-Modified: Mon, 02 Mar 2015 07:07:04 GMT
Expires: Wed, 01 Apr 2015 07:46:46 GMT
Cache-Control: max-age=2592000
(6).问题
区别: 在使用绝对路径的时候没办法反向代理。只能请求真实的服务器。//request.getContextPath();
<base href="/SpringmvcMybatis/">
// request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
<base href="http://localhost:80/SpringmvcMybatis/">
默认生成的base 标签使用的是绝对路径。不留神下,还是会穿过代理直接访问真实服务器。
参考 :
http://www.nginx.cn/nginx-download
http://www.linuxidc.com/Linux/2014-07/104040.htm
http://nginx.org/cn/