Nginx 1.11.1 安装及基本使用

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的图片

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值