Detailed Nginx server's nginx-http-footer-filter module configuration _nginx

Source: Internet
Author: User
Tags nginx server

Nginx-http-footer-filter Presumably everyone feels very strange, then we come to know it, this is the nginx of Taobao development module. It is used to nginx append content at the bottom of the response request file. Today, the time to study the plug-in, I hope to help you. Why found this plugin, because these days the company needs to be in all shtml files appended a JS code to do statistics (before the statistics code is not fully added), found it in the process of finding a solution and realized it finally liked it, you might think that I use this plugin to achieve the function I want, I actually replaced it with a shell script before I knew him. However, I decided to study and test the Nginx-http-footer-filter, perhaps later on the need for help, but also may be able to help other people in need of help. Let's get to the point.
1. What exactly does Nginx-http-footer-filter do?
to put it bluntly, insert the code you want to insert at the bottom of the requested page.
2. What can we do with nginx-http-footer-filter?
1, the unified additional JS code for statistics (I think so)
2, the bottom of the additional response to the request of the Realsver (back-end real server) information, easy to system administrators troubleshooting.
3, you manage a large number of virtual host, in the back of all the Web to append your advertising code, black chain or something (very shameless)
4, extrapolate, think about what you can do for yourself.
What does Taobao use it for?
Open Taobao home page, look at his source code, and drag to the bottom, the contents are as follows:

<!--city:fuzhou-->
<!--province:unknown-->
<!--hostname:-->
<!--hostname: Home1.cn199-->

We can clearly see that there are provinces and regions and the host name, that is, Taobao Real server host name, processing my request for the host named Home1.cn199, City took to the Fuzhou,provinece province did not take, it is estimated that the problem of GEO
or casually open a product page, view the source code, as follows:

 
 

Can see his side to this page to append a JS code, Taobao development of this module is presumably everyone understand, brainstorming, perhaps we have better use.
3. How to install Nginx-http-footer-filter
3.1 Download Address:

https://github.com/alibaba/nginx-http-footer-filter/tree/1.2.2
3.2 Installing Nginx-footer Module
Nginx has been installed before, so I chose to overwrite Nginx file.

# cd/usr/local/src/#
wget https://codeload.github.com/alibaba/nginx-http-footer-filter/zip/1.2.2 #
Unzip 1.2.2 #
 
http://nginx.org/download/nginx-1.4.1.tar.gz #
TAR-XZVF nginx-1.4.1.tar.gz
# CD nginx-1.4.1
#./configure--prefix=/usr/local/nginx-1.4.1 \
--with-http_stub_status_module--with-http_realip_module \
--add-module=.. /nginx-http-footer-filter-1.2.2
# make
# Mv/usr/local/nginx-1.4.1/sbin/nginx/usr/local/nginx-1.4.1/sbin /old_nginx #
MV objs/nginx/usr/local/nginx-1.4.1/sbin/#
/usr/local/nginx-1.4.1/sbin/nginx-s Stop
# /usr/local/nginx-1.4.1/sbin/nginx

3.3 Verify that the module is installed successfully

#/usr/local/nginx-1.4.1/sbin/nginx-v
nginx version:nginx/1.4.1
built by gcc 4.4.7 20120313 (Red Hat 4.4.7-3) (G CC)
TLS SNI Support enabled
Configure arguments:--prefix=/usr/local/nginx-1.4.1 
--with-http_stub_ Status_module 
--with-http_realip_module 
--add-module=. /nginx-http-footer-filter-1.2.2

4. How to use the Nginx-http-footer-filter module
4.1 Configuration Location
Use footer "Your content" in location. Look at the following configuration

server {
    listen    173.255.219.122:80;
    server_name test.ttlsa.com;
    Access_log/data/logs/nginx/test.ttlsa.com.access.log main;
 
    Index index.html index.php index.html;
    root/data/site/test.ttlsa.com;
    Location/{
      Footer "<!--$date _gmt-->";
      Index index.html;
    }
 
    Location =/html/2252.css {
      footer_types text/css;
      Footer "/* Host: $server _name-$date _local *";
}

4.2 Test Nginx-footer Effect

# cat 2252.shtml
 
 

Visit Site Test.ttlsa.com/html/2252.shtml

As shown, we can see the bottom of the file plus the <!--1376063369, how to become a time pinch, because my side is SSI syntax, if you do not know what is SSI, then please refer to the article what is SSI.
[WARNING] He only appends the last line of the file, not the <body>. We should pay attention to this point. [/warning]
4.3 To test the CSS file again

# cat 2242.css
# This is CSS file

The following are the results of the visit:

# This is CSS file/
* host:test.ttlsa.com-1376064324 * *

Look at the picture:

5. Can I write multiple footer instructions?
No, I wrote two footer here.

Location/{
  footer "12312321321";
  Footer "<!--$date _gmt-->";
  Index index.html;
}

The following test prompts the footer instruction to repeat

#/usr/local/nginx-1.4.1/sbin/nginx-t
nginx: [Emerg] "footer" directive is duplicate IN/USR/LOCAL/NGINX-1.4.1/ Conf/vhost/test.ttlsa.com.conf:13
nginx:configuration file/usr/local/nginx-1.4.1/conf/nginx.conf test failed

6. Can I use SSI variables only?
of course not, whatever you write, it can be an SSI instruction, it can be a nginx variable, or it can be any meaningless string.
As follows:

Footer "12312321321";
Footer "<!--12312321321-->";
Footer "<!--$remote _addr-->";

For example, I want to know which Web server This page is processing, then I insert the host name at the bottom. So, with 500 errors, I can immediately locate a specific server.

Footer "<!--$hostname-->";

The returned results are as follows:

7. Server return 500,404,403 error, whether will append content to the bottom
, if you do not append, you will not be able to return to the page to know which Web failure, which is obviously not in line with the original intention of the author
The configuration is as follows:

Location/{return
  ;
  Footer "<!--$hostname-->";
}

The results are as follows:

8. Module Instruction Description:
the footer module is very simple, there are only two instructions, specifically described below
Footer string
Default value:
Configuration segment: HTTP, server, location
This defines what content is appended to the bottom of the file's contents
Footer_types MIME Type
Default value: footer_types:text/html
Configuration segment: HTTP, server, location
Defines the MIME return type of the appended bottom file, the default value is text/html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.