首页 > 技术文章 > nginx ssi 配置小细节(一)

shihuc 原文

最近工作需要使用nginx的ssi (server side include)技术,在这里,将使用中的一点心得分享一下,也是一种备忘!

首先,nginx的ssi启用很简单,就只有三个最基本的指令:

ssi on;         默认是关闭的

ssi_silent_errors on;  默认是关闭的

ssi_types text/shtml;  默认是text/html

这三行的配置可以放在nginx的三个作用域下面(http, server, location)。nginx的基本概念可以自己去网上学习。

这里,重点介绍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等指令之间有至少一个空格。后面的-->与前面的内容之间最好分开,不要连在一起,养成一个好的编码习惯。

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

推荐阅读