Nginx之SSI

SSI是什么:Server Side Include,是一种基于服务端的网页制作技术。

The ngx_http_ssi_module module is a filter that processes SSI (Server Side Includes) commands in responses passing through it.

它的工作原因是:在页面内容发送到客户端之前,使用SSI指令将文本、图片或代码信息包含到网页中。对于在多个文件中重复出现内容,使用SSI是一种简便的方法,将内容存入一个包含文件中即可,不必将其输入所有文件。通过一个非常简单的语句即可调用包含文件,此语句指示 Web 服务器将内容插入适当网页。而且,使用包含文件时,对内容的所有更改只需在一个地方就能完成.

上面都是官方或者抄袭的说法,官方有一句话  Currently, the list of supported SSI commands is incomplete。

简单说,就是在内容回到客户端前,通过conf的配置对内容进行额外的处理。这种技术 Apache和IIS都有的。

我们进行一个最简单的演示,就是在系统默认首页 index.html添加一个header.html和footer.html的引用。

前提条件是已经安装 nginx-1.9.9到 c:\nginx-1.9.9.如果不知道如何安装,请看前面的文章。

1. 打开 c:\nginx-1.9.9\conf\nginx.conf,启用SSI,

我是在 server下面启用的,当然你也可以在location下面,添加代码,第一行驶启用,第二行是对错误的处理,这里有额外的修改,端口修改为2016.

在server 添加代码:

  ssi on;

  ssi_slient_errors on;

  

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  ‘$remote_addr - $remote_user [$time_local] "$request" ‘
    #                  ‘$status $body_bytes_sent "$http_referer" ‘
    #                  ‘"$http_user_agent" "$http_x_forwarded_for"‘;

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       2016;
        server_name  localhost;
	ssi on;
	ssi_silent_errors on;	

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache‘s document root
        # concurs with nginx‘s one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }

    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}

  

2. 打开 c:\nginx-1.9.9\html\index.html,修改成如下:

修改就是内容前面和后面添加了

<!--#include virtual="header.html" -->

<!--#include virtual="footer.html" -->

<!DOCTYPE html>
<html>
<head>
    <title>Welcome to nginx!</title>
    <style>
        body {
            width: 35em;
            margin: 0 auto;
            font-family: Tahoma, Verdana, Arial, sans-serif;
        }
    </style>
</head>
<body>

    <!--#include virtual="header.html" -->

    <h1>Welcome to nginx!</h1>
    <p>
        If you see this page, the nginx web server is successfully installed and
        working. Further configuration is required.
    </p>
    <p>
        For online documentation and support please refer to
        <a href="http://nginx.org/">nginx.org</a>.<br />
        Commercial support is available at
        <a href="http://nginx.com/">nginx.com</a>.
    </p>
    <p><em>Thank you for using nginx.</em></p>

    <!--#include virtual="footer.html" -->
</body>
</html>

3. 在c:\nginx-1.9.9\html下面创建header.html,内容为:

<div>
Content in head!
</div>

4. 在 c:\nginx\1.9.9\html下面创建 footer.html,内容为:

<div>
Content in footer!
</div>

5. 打开cmd,

cd c:\nginx-1.9.9

start nginx

6. 打开浏览器,输入 http://localhost:2016

内容就变成:

Content in head!

Welcome to nginx!

If you see this page, the nginx web server is successfully installed and working. Further configuration is required.

For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.

Thank you for using nginx.

Content in footer!

参考文章:

http://nginx.org/en/docs/http/ngx_http_ssi_module.html#ssi

How to Setup HTML Server Side Includes SSI on Apache and Nginx
http://www.thegeekstuff.com/2014/07/server-side-includes/

Dynamic SSI Example

https://www.nginx.com/resources/wiki/start/topics/examples/dynamic_ssi/

时间: 2024-08-26 08:07:53

Nginx之SSI的相关文章

CentOS 6.5 Nginx 的编译安装、以及让nginx 支持 SSI 相对路径写法

1. nginx 下载 进入 http://nginx.org/en/download.html 下载,我是选择的 "Stable version" 稳定版本的下载 # wget http://nginx.org/download/nginx-1.6.2.tar.gz 2. 解压编译安装 # tar -zxvf nginx-1.6.2.tar.gz # cd nginx-1.6.2 安装一些nginx用到的依赖软件,如果不开启ssl可以不用安装openssl,回头用到时可以返回这里重新

nginx支持ssi使用../

让nginx支持ssi,只需要在http段里增加 ssi on; ssi_types text/shtml; 或者在需要支持的虚拟主机配置里增加也可以.server段里. 但是相对apache来说,nginx更加严格,对于include virtual=../../ 这样的使用方法是不支持的,会在error日志中报错"unsafe URI".那么nginx就不能做到这一点了吗? 打开nginx的ssi源码src/http/modules/ngx_http_ssi_filter_modu

nginx中SSI问题的研究

最近感觉挺爽的,这个项目团队没有一个专门做PHP的,我是第一个进来做PHP(当然还有前端)的,哈哈,我会设计修改出适合我们业务的PHP框架,哈哈,感觉会学到很多东西的样子,前几天在组内20几个前辈面前讲php框架,以及跟大牛们探讨适合我们的php框架,感觉表达能力太差了,知道的东西表达不清楚,还要我的导师去帮我表达,这里感谢下我的导师于洪磊(简称磊哥),磊哥简直就是我的偶像,我没见过那么有深度的程序员,技术牛B这是肯定的了,对技术外的了解超出了我的想象,磊哥看的书很多,涉猎很广泛,尤其在历史和文

SSI简介 与 nginx开启SSI

Server Side Include : 服务器端嵌入 原理 : 将内容发送到浏览器之前,可以使用“服务器端包含 (SSI)”指令将文本.图形或应用程序信息包含到网页中.因为包含 SSI 指令的文件要求特殊处理,所以必须为所有 SSI 文件赋予 SSI文件扩展名.默认扩展名是 .stm..shtm 和 .shtml 主要有以下几种用用途: 1.显示服务器端环境变量<#echo> 2.将文本内容直接插入到文档中<#include> 3.显示WEB文档相关信息<#flastmo

nginx配置ssi实现页面拆分

在做一个网站时,页面上会有很多重复的内容,每个页面写一遍很冗余,修改时还容易遗漏,所以可以把公共的部分写好,放在单独的HTML中,用时引用就行了. nginx配置ssi可以将单个页面拆分成一个一个的小页面,访问页面时将多个子页面合并渲染输出,通过cms去管理这些小页面,实现当要更改部分页面内容时只需要更改具体某个小页面. 1. 什么是SSI SSI:Server Side Include,是一种基于服务端的网页制作技术,大多数(尤其是基于Unix平台)的web服务器如Netscape Enter

apache和nginx支持SSI配置

一. 前言 SSI是一种类似于ASP的基于服务器的网页制作技术.将内容发送到浏览器之前,可以使用“服务器端包含 (SSI)”指令将文本.图形或应用程序信息包含到网页中.例如,可以使用 SSI 包含时间/日期戳.版权声明或供客户填写并返回的表单.对于在多个文件中重复出现的文本或图形,使用包含文件是一种简便的方法.将内容存入一个包含文件中即可,而不必将内容输入所有文件.通过一个非常简单的语句即可调用包含文件,此语句指示 Web 服务器将内容插入适当网页.而且,使用包含文件时,对内容的所有更改只需在一

Nginx配置SSI

一.什么是SSISSI:Server Side Include,是一种基于服务端的网页制作技术,大多数(尤其是基于Unix平台)的web服务器如Netscape Enterprise Server等均支持SSI命令.它的工作原因是:在页面内容发送到客户端之前,使用SSI指令将文本.图片或代码信息包含到网页中.对于在多个文件中重复出现内容,使用SSI是一种简便的方法,将内容存入一个包含文件中即可,不必将其输入所有文件.通过一个非常简单的语句即可调用包含文件,此语句指示 Web 服务器将内容插入适当

让nginx 的ssi支持include相对路径

好久没接触nginx了,今天帮同事解决一个客户的问题,顺便记录下:version : nginx-1.6.2问题描述:客户的files.shtml里面include一个网站的头部文件( <!–# include file=”../../woqu.htm”–>),采用的是相对路径,这样的写法在apache里面可以正常访问,但是在nginx里面不能正常加载日志上提示:unsafe URI "../../woqu.html" was detected while sending

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的基本概念可以自己去网上学习. 这里