Nginx配置虚拟机,url重写,防盗链

配置目录:

·     虚拟主机

·     PHP支持

·     URL重写

·     防止盗链

·     持续更新…


一、虚拟主机

1、创建

文件格式:{域名}.conf

具体如下:

$ sudo mkdir –pv /usr/local/nginx/conf/vhost

$ sudo touch /usr/local/nginx/conf/vhost/test1.cwteam.com.conf

$ sudo touch /usr/local/nginx/conf/vhost/test2.cwteam.com.conf

在这里按照格式创建两个虚拟主见的配置文件。另外,我们还需新建对应的虚拟主机服务的root位置:

$ sudo mkdir –pv /nginx/proxy/test1_cwteam_com

$ sudo mkdir –pv /nginx/proxy/test2_cwteam_com

2、配置

这里做简单配置,详细配置可参看nginx.conf配置,具体如下:

$ sudo vim/usr/local/nginx/conf/vhost/test1.cwteam.com.conf

server {

 listen 8080;

 server_name test1.cwteam.com;

 index index.html index.htmindex.php;

 root /nginx/proxy/test1_cwteam_com;

 error_log off;

 access_log off;

}

$ sudo vim /usr/local/nginx/conf/vhost/test2.cwteam.com.conf

server {

 listen 8081;

 server_name test2.cwteam.com;

 index index.html index.htmindex.php;

 root /nginx/proxy/test2_cwteam_com;

 error_log off;

 access_log off;

}

最后,我们在nginx.conf中的http{}内,server{}外引入虚拟机配置并重启nginx服务即可:

include vhost/*.conf;

注意:

别忘了在/etc/hosts中添加:

127.0.0.1   test1.cwteam.com

127.0.0.1   test2.cwteam.com

3、测试

为了测试方便,我们这里分别在test1_cwteam_com和test2_cwteam_com中放入对应的index.html文件,链接测试下两个虚拟主机:

$curl test1.cwteam.com:8080

$curl test2.cwteam.com:8081

 

好了,虚拟主机的配置已经好了,接下来在此基础上对配置下对PHP的支持。

二、PHP支持

1、添加配置

我们为上面的虚拟机test1添加对PHP支持,虚拟机配置:

server {

 listen 8080;

 server_name test1.cwteam.com;

 index index.html index.htmindex.php;

 root /nginx/proxy/test1_cwteam_com;

 location ~ .php { 

            #fastcgi_pass   unix:/tmp/php-cgi.sock;

            fastcgi_pass   127.0.0.1:9000; 

            fastcgi_index  index.php; 

            fastcgi_intercept_errorson;

            #设置PATH_INFO,注意fastcgi_split_path_info已经自动改写了fastcgi_script_name变量, 

            #后面不需要再改写SCRIPT_FILENAME,SCRIPT_NAME环境变量,所以必须在加载fastcgi.conf之前设置 

           fastcgi_split_path_info ^(.+.php)(/.*)$; 

            fastcgi_param  PATH_INFO $fastcgi_path_info; 

             

            #加载Nginx默认"服务器环境变量"配置 

            include        fastcgi.conf; 

        } 

 error_log off;

 access_log off;

}

配置完成之后,重新加载nginx服务,使用nginx –t测试下配置是否正确。

2、测试虚拟

首先,在test1服务器服务的根目录下添加一个测试index.php文件,内容如下:

<?php

 echo 'Hello Test1 You can supportthe PHP!';

?>

其次,在浏览器中输入http://test1.cwteam.com:8080/index.php 刷新,结果如下:

三、URL重写

Nginx的URL重写是基于Pcre规则,Perl负责兼容正则表达式的,如果需要开启Rewrite规则,需要先安装Pcre库。另外,使用Rewrite可以定制规范的URL及根据变量来做URL转向,具体如下:

1、安装pcre库

$ sudo brew install pcre

2、指令语句

Rewrite支持的指令集包括:

A、rewrite(重写指令)

语法规则:

        ~ 符号表示区分大小写字母匹配

       ~* 符号表示不区分大小写字母匹配

     !~ 和 !~ 与~  !~ 相反

     -f 和 !-f   用来判断文件是否存在

     -d 和 !-d   用来判断目录是否存在

     -e 和 !-e   用来判断文件或目录是否存在

     -x 和 !-x   用来判断文件是否可以执行

支持$1到$9位置参数

变量名可以使用"="或"!="运算符

B、if(判断指令)

语法规则:

       if (!-f$request_filename){  

    rewrite ^/test/(.*)$/site/$host/images/$1 last;  

      }

C、return(状态码返回指令)

语法规则:

        if ($forbidden){  

             return 403;

      }

D、示例

location / {

   root/nginx/proxy/test2_cwteam_com;

   index index.html index.php;

   #AAA

   if (-f$request_filename/index.html){

        rewrite (.*) $1/index.htmlbreak;

   }

   if (-f$request_filename/index.php){

       rewrite (.*) $1/index.php;

   }

   if (!-f $request_filename){

       rewrite (.*) /index.php;

   }

  #BBB

   rewrite ^/test2/(.*)$ /form/$1;

 }

注:

#AAA部分判断如果访问的URL中含有index.html index.htm index.php的话,访问时可以不用补全index.*部分。

#BBB部分代表访问test2时,直接跳转到form下了。

四、防止盗链(图片)

location ~ .(jp?g|png|gif|bmp)$ {

     valid_referers none blocked mysite.com *.mysite.com;

     if ($invalid_referer) {

        return   403;

    }

}

注:

valid_referers-允许访问资源的网站列表,不在列表中请求的返回403;

none -匹配没有Referer的HTTP请求;

blocked -请求有Referer,但是被防火墙或者代理服务器修改,去掉https://或http://

*.domain.com -匹配mysite.com的所有二级域名;

location /images/ {

     valid_referers none blocked mysite.com *.mysite.com;

     if ($invalid_referer) {

        return   403;

    }

}

注:防止访问images下的所有文件

原博文地址:http://blog.csdn.net/why_2012_gogo/article/details/50967447

特别感谢原博主对本人学习带来的帮助。

原文地址:https://www.cnblogs.com/wangzhilei/p/6706125.html