码迷,mamicode.com
首页 > 其他好文 > 详细

Nginx之SSI

时间:2015-12-20 14:36:41      阅读:243      评论:0      收藏:0      [点我收藏+]

标签:

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/

Nginx之SSI

标签:

原文地址:http://www.cnblogs.com/mouse_in_beijing/p/5060813.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!