1.安装编译文件及库
yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel
2.安装PCRE,Nginx的rewrite的伪静态匹配规则需要用到正则表达式,PCRE就是起到这个作用。
wget http://downloads.sourceforge.net/project/pcre/pcre/8.35/pcre-8.35.tar.gz
tar -zxvf pcre-8.35.tar.gz
cd pcre-8.35
./configure
make && make install
3.nginx的安装
下载包
wget http://nginx.org/download/nginx-1.11.1.tar.gz
tar -zxvf nginx-1.11.1.tar.gz
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-pcre=/usr/local/pcre/pcre-8.35
–prefix= nginx的安装路径
–with-pcre=pcre的源码路径
make
make install
./nginx -v
查看nginx版本
4.Nginx的基本使用
基本使用就不说了,下文都会提现到
1. 重定向测试
nginx.conf文件的配置,这里只介绍简单的server部分:
server {
listen 80;# //监听80端口
server_name www.nihao.com; #//监听域名 ,可为多个中间用空格隔开,可用*进行模糊匹配,可匹配正则表达式
location / {
if ($query_string ~* "category=(\d+)$") {
set $id $1 ;
rewrite ^/product/list.html http://www.test.com/product/list/$id? permanent;//重定向url
}
}
#rewrite ^/product/list.html?category=(\d+)$ http://www.test.com/product/list/$1 last;
#rewrite ^/(.*) http://www.test.com/$1 last;
}
server {
listen 80;# //监听80端口
server_name www.test.com; #//监听域名 ,可为多个中间用空格隔开,可用*进行模糊匹配,可匹配正则表达式
location / {#//根目录下默认打开/usr目录的index.html,如果index.html不存在的话打开index.htm文件
if ($uri ~* "/product/list/(\d+)" ) {//如果是上文重定向过来的话,做一个特殊处理
proxy_pass http://127.0.0.1:8989/product/list.html?category=$1;
}
if ($uri !~* "/product/list/(\d+)" ) {//如果不是请求的重定向定制操作的话就请求到主服务器
proxy_pass http://127.0.0.1:8989;
}
}
}
上面的nginx配置为了做重定向的问题,我故意复杂化了,故意定了一个nihao的域名,然后访问参数为category=12的请求的时候,重定向到http://www.test.com/product/list/12的url下。然后再test服务的请求里面做了拦截url请求到某个tomcat的请求的处理
2. nginx静态资源防盗链
server {
listen 80 ;
server_name static.test.com;
location ~.(jpg)$ {#静态资源访问
root /usr;
valid_referers none blocked www.test.com static.test.com;
if ($invalid_referer) {
rewrite ^/ http://www.test.com/default/404.jpg last;
}
}
}
上文的配置,只允许请求为www.test.com域名的网站用static.test.com的图片,其他的都如果请求过来了都会被转到一个404的图片