nginx ssi 配置小细节(一)
最近工作需要使用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的使用。
其他不多说,直接上配置:
upstream wxcj-server {
server 10.90.7.1:8082;
server 10.90.7.2:8082;
ip_hash;
}
server {
listen 82;
server_name localhost;
ssi on;
ssi_silent_errors off;
ssi_types text/html;
access_log logs/wxcj-test.log;
#location ~ \.(shtm|shtml)$ {
# root /var/wxcj;
# index index.shtml;
#}
location /tests {
rewrite /tests/(\w+)/(\w+).html /option/$1.html?product_id=$2;
set $product_id $arg_product_id;
}
location /option {
root /var/wxcj/cms/;
#set $product_id $arg_product_id;
}
location /product {
root /var/wxcj/cms/;
#set $product_id $arg_product_id;
}
location / {
proxy_pass http://wxcj-server;
proxy_redirect off;
proxy_set_header Host $host:$server_port;
proxy_set_header Remote_Addr $remote_addr;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
红色部分是这里要讲的重点内容。这里都是介绍静态文件的访问配置,至于动态的文件,是通过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路径如下:
[root@localhost cms]# pwd
/var/wxcj/cms #本例中配置的root
[root@localhost cms]# ll
总计
drwxr-xr-x root root - : option #/option虚拟路径
drwxr-xr-x root root - : 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下面的内容:
[root@localhost cms]# cd option/
[root@localhost option]# ll
总计
-rw-r--r-- root root - : s1001.html
-rw-r--r-- root root - : s1002.html
-rw-r--r-- root root - : s2001.html
-rw-r--r-- root root - : s2002.html
-rw-r--r-- root root - : s2003.html
再看看s1001.html的内容:
<div>
this is an option A
</div>
<!--# echo var="product_id" -->
<!--# include virtual="/product/$product_id.html" -->
另外动态参数product_id.html的内容,在这个例子里面,这个文件是1000.html,其内容如下:
<p> this is a title for product </p>
注意,上面的virtual等号右边的格式,是相对于root路径的一个绝对路径写法,也可以理解为虚拟路径。上面的echo指令后面的var等号右边,必须是变量的名字,不要带上$这个符号。还有一点就是ssi指令的语法,<!--#是一个整体,否则会造成ssi指令的内容解析不出来的问题。与后面的内容比如echo,include等指令之间有至少一个空格。后面的-->与前面的内容之间最好分开,不要连在一起,养成一个好的编码习惯。
最后浏览器打开后的效果如下:

nginx ssi 配置小细节(一)的更多相关文章
- win php nginx 配置小细节
win下配置php Nginx 首先 下载 php-Windows版本.下载Nginx Windows 版本 1> php.ini-production 修改为 php.ini 让其成为php的 ...
- wamp配置小细节
1. 问题:在安装后,把phpMyadmin改密码后,再次登陆会提示出错.访问被拒绝. 原因:这是因为WampServer设置了直接登陆. 解法:修改config.inc.php文件中$cfg['Se ...
- Nginx return 关键字配置小技巧
Nginx的return关键字属于HttpRewriteModule模块: 语法:return http状态码 默认值:无 上下文:server,location,if 该指令将结束执行直接返回htt ...
- 微信小程序Nginx环境配置
环境配置概述 主要内容: SSL免费证书申请步骤 Nginx HTTPS 配置 TLS 1.2 升级过程 微信小程序要求使用 https 发送请求,那么Web服务器就要配置成支持 https,需要先申 ...
- nginx配置 yii2 URL重写规则 SSI配置使shtml
location / { // 加上红色部分 重写url try_files $uri $uri/ /index.php?$args; if (!-e $request_filename){ rewr ...
- nginx ssi 模块
在nginx下与SSI配置相关的参数主要有ssi ssi_sclient_error ssi_types三个.具体的用法如下 ssi on 开启ssi支持,默认是off ssi_silent_err ...
- Oracle Sales Cloud:管理沙盒(定制化)小细节1——利用公式创建字段并显示在前端页面
Oracle Sales Cloud(Oracle 销售云)是一套基于Oracle云端的CRM管理系统.由于 Oracle 销售云是基于 Oracle 云环境的,它与传统的管理系统相比,显著特点之一便 ...
- 从零开始学 Java - CentOS 下 Nginx + Tomcat 配置负载均衡
为什么现在有非常多的聪明人都在致力于互联网? 最近在读埃隆·马斯克传记,他说「我认为现在有非常多的聪明人都在致力于互联网」. 仔细一想,好像真的是这样的. 我问了自己一个问题:如果你不敲代码了,你能做 ...
- Nginx主配置参数详解,Nginx配置网站
1.Niginx主配置文件参数详解 a.上面博客说了在Linux中安装nginx.博文地址为:http://www.cnblogs.com/hanyinglong/p/5102141.html b.当 ...
随机推荐
- C++学习笔记5:如何给变量及函数命名?
1.遵循C++规定的变量及函数命名方法: 2.原则:简单,易于理解: 以下是一些例子,可以作为参考: //bad examples: int ccount;//Nobody knows what a ...
- Tomcat9源码编译及导入Eclipse(转)
1.下载tomcat源码.建议下载最新版本tomcat9. svn地址:http://svn.apache.org/repos/asf/tomcat/tc9.0.x/branches/gsoc-jas ...
- day10_面向对象第五天
1.包(掌握) 1.概念(掌握) 包就是文件夹,用于区分相同的类名 2.声明格式 package 包名1.包名2-.; package:是个关键字3.带包的类的编译和运 ...
- dedecms头部标签(标题,关键词,描述标签)(借用)
先说说dedecms头部标题,关键词,描述标签的作用我相信网络上也有很多这样的信息,那为什么我还要写这个?因为这个对我们初学者来说还是比较重要的,因为做SEO就要用到这些标签.首先我写下首页头部标签我 ...
- textarea元素在加上runat="server"后运行报错解决
当出现这个报错的时候,在后台引用相应的命名空间,为 using System.Web.UI; using System.Web.UI.HtmlControls;using System.Web.UI. ...
- WEBRTC源码片段分析(1)音频缓冲拷贝
源码位置webrtc/webrtc/modules/audio_device/ios/audio_device_ios.cc函数OSStatus AudioDeviceIPhone::RecordPr ...
- java计算文件32位md5值
protected static String getFileMD5(String fileName) { File file = new File(fileName); if(!file.exist ...
- A Gentle Introduction to the Gradient Boosting Algorithm for Machine Learning
A Gentle Introduction to the Gradient Boosting Algorithm for Machine Learning by Jason Brownlee on S ...
- C++ code: 将程序的输出,保存到txt文档中,且每35个数,自动换行
// write the predicted score into txt files ofstream file("/home/wangxiao/Downloads/caffe ...
- JSBinding / Run Samples
This document shows you how to run JSBinding 2048 sample in Editor. First of course, create an empty ...