Nginx配置SSI

一.什么是SSI
SSIServer Side Include,是一种基于服务端的网页制作技术,大多数(尤其是基于Unix平台)的web服务器Netscape Enterprise Server等均支持SSI命令。

用个例子来说明,一个静态化的页面中,需要嵌入一小块实时变化的内容,。例如首页,大部分的页面内容需要缓存但是用户登录后的个人信息是动态信息,不能缓存。那么如何解决这个”页面部分缓存”问题,利用SSI就可以解决,在首页的静态页面中嵌入个人信息的动态页,由于是服务器端的嵌入,所以用户浏览的时候都是一个嵌入后的页面。
它的工作原因是:在页面内容发送到客户端之前,使用SSI指令将文本、图片或代码信息包含到网页中。对于在多个文件中重复出现内容,使用SSI是一种简便的方法,将内容存入一个包含文件中即可,不必将其输入所有文件。通过一个非常简单的语句即可调用包含文件,此语句指示 Web 服务器将内容插入适当网页。而且,使用包含文件时,对内容的所有更改只需在一个地方就能完成。

二.如何在nginx上配置SSI
需要的选项主要是以下三个:
ssi: 默认值off,启用ssi时将其设为on
ssi_silent_errors: 默认值off,开启后在处理SSI文件出错时不输出错误提示"[an error occurred while processing the directive]"。
ssi_types: 默认是text/html,所以如果需支持html,则不需要设置这句,如果需要支持shtml则需要设置:ssi_types text/shtml
三个参数可以放在http, server或location作用域下。

三. 实例

[plain]  view plain copy
  1. server {  
  2.     listen  10.3.9.27:80;  
  3.     server_name  www.ball.com;  
  4.     location / {  
  5.         ssi on;  
  6.         ssi_silent_errors on;  
  7.         ssi_types text/shtml;  
  8.         index index.shtml;  
  9.         root /usr/local/web/wwwroot;  
  10.         expires 30d;  
  11.         access_log      /data/logs/www.ball.com-access_log main;  
  12.     }  
  13. }  


了解更多SSI配置及命令请猛击这里

页面上配置

  1. <!–# include file=”/m/2013/4/15/index_top.html”–>  

这里,重点介绍nginx ssi的include之virtual的使用!下一博文介绍include之file的使用。

其他不多说,直接上配置:

 1 upstream wxcj-server {
 2     server 10.90.7.1:8082;
 3     server 10.90.7.2:8082;
 4     ip_hash;
 5 }
 6 
 7 server {
 8         listen       82;
 9         server_name  localhost;
10 
11         ssi on;
12         ssi_silent_errors       off;
13         ssi_types       text/html;
14 
15         access_log  logs/wxcj-test.log;
16 
17         #location ~ \.(shtm|shtml)$ {
18         #       root /var/wxcj;
19         #       index index.shtml;
20         #}
21 
22         location /tests {
23                 rewrite /tests/(\w+)/(\w+).html  /option/$1.html?product_id=$2;
24                 set $product_id $arg_product_id;
25         }
26 
27         location /option {
28                 root /var/wxcj/cms/;
29                 #set $product_id $arg_product_id;
30         }
31         location /product {
32                 root /var/wxcj/cms/;
33                 #set $product_id $arg_product_id;
34         }
35 
36         location / {
37            proxy_pass http://wxcj-server;
38            proxy_redirect off;
39            proxy_set_header Host $host:$server_port;
40            proxy_set_header Remote_Addr $remote_addr;
41            proxy_set_header X-Real-IP $remote_addr;
42            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
43 
44           }
45       }

红色部分是这里要讲的重点内容。这里都是介绍静态文件的访问配置,至于动态的文件,是通过upstream以及proxy-pass部分实现的,不是这里介绍的内容,略去!

静态部分,有两个虚拟路径/option,一个是/product,这两个涉及到实际访问时url的一部分。另外的一个重点,是22-25行的配置,这里有rewrite以及set两个重要的指令。

1》 rewrite重写url,这里,将类似/tests/a/b.html的请求重写为/option/a.html?product_id=b这样子的url。此时,http请求将进入到/option/虚拟路径下面,注意,这里的/option以及/product这两个虚拟路径都是相对root配置的路径而言的,root路径是绝对的路径。本测试中的root路径和/option以及/product路径如下:

1 [root@localhost cms]# pwd
2 /var/wxcj/cms      #本例中配置的root
3 [root@localhost cms]# ll
4 总计 8
5 drwxr-xr-x 2 root root 4096 06-14 09:10 option  #/option虚拟路径
6 drwxr-xr-x 2 root root 4096 06-13 18:29 product  #/product虚拟路径

2》 set指令,在这里也非常关键,是用来设置nginx的应用变量的。这个变量可以传递到ssi指令的解析过程中。 set $product_id $arg_product_id这个是将rewrite指令中?符号后面的变量的值通过$arg_product_id取到付给变量$product_id, 在nginx中取url中?后面的变量的值是通过$arg_作为前缀获取的。 例如/abc/123?name="9527", 那么,set $yourname $arg_name指令就可以将这个url中的name变量的值9527赋值给变量$yourname.

 

下面看看我的测试页面,我的url是http://10.90.7.1:82/tests/s1001/1000.html,nginx首先rewrite成为http://10.90.7.1:82/option/s1001.html?product_id=1000这个样子。先看看option下面的内容:

1 [root@localhost cms]# cd option/
2 [root@localhost option]# ll
3 总计 20
4 -rw-r--r-- 1 root root 114 06-14 09:00 s1001.html
5 -rw-r--r-- 1 root root 120 06-14 09:08 s1002.html
6 -rw-r--r-- 1 root root 127 06-14 09:10 s2001.html
7 -rw-r--r-- 1 root root  94 06-13 18:03 s2002.html
8 -rw-r--r-- 1 root root  94 06-13 18:03 s2003.html

再看看s1001.html的内容:

1 <div>
2         this is an option A
3 </div>
4 <!--# echo var="product_id" -->
5 <!--# include virtual="/product/$product_id.html" -->

另外动态参数product_id.html的内容,在这个例子里面,这个文件是1000.html,其内容如下:

1 <p> this is a title for product 1000</p>

注意,上面的virtual等号右边的格式,是相对于root路径的一个绝对路径写法,也可以理解为虚拟路径。上面的echo指令后面的var等号右边,必须是变量的名字,不要带上$这个符号。还有一点就是ssi指令的语法,<!--#是一个整体,否则会造成ssi指令的内容解析不出来的问题。与后面的内容比如echo,include等指令之间有至少一个空格。后面的-->与前面的内容之间最好分开,不要连在一起,养成一个好的编码习惯。

最后浏览器打开后的效果如下:

猜你喜欢

转载自2277259257.iteye.com/blog/2317808
ssi