crul新浪微博的时候发现对面用的是nginx服务器,在虎扑足球(挺好的足球论坛)讨论世界杯也发现他们也用这nginx,联想到阿里的tengine也是基于nginx的,觉得有了解一下nginx的必要了。

1.  nginx安装

  nginx安装跟普通开源软件安装相似(都使用makefile),具体流程:下载源文件,./configure,make,make install。

  在./configure过程中,会出现一些找不到一些依赖组件的提示,自行安装即可。可以通过源码编译方式安装对应组件。也可以通过系统自带的安装方式进行,比如在使用fedora安装相关openssl组件时,

  sudo yum install openssl-devel  这里会添加相应的编程关联文件,如果仅仅是sudo yum install openssl  就可能出现错误。

  ./configure过程中存在一些比较实用的参数,如

  ./configure --prefix=/../.. --with-http_ssl_module --with-openssl=/../.. --add-module=/home/..

  这些参数不加的话也会设置相应的默认值。

2.  程序

  nginx_http_mytest_module.c

#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_http.h> static ngx_int_t ngx_http_mytest_handler(ngx_http_request_t *r);
static char *
ngx_http_mytest(ngx_conf_t *cf, ngx_command_t *cmd, void *conf); static ngx_command_t ngx_http_mytest_commands[] = {
{
ngx_string("mytest"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LMT_CONF|NGX_CONF_NOARGS,
ngx_http_mytest,
NGX_HTTP_LOC_CONF_OFFSET,
,
NULL
},
ngx_null_command
}; static ngx_http_module_t ngx_http_mytest_module_ctx = {
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL
}; ngx_module_t ngx_http_mytest_module = {
NGX_MODULE_V1,
&ngx_http_mytest_module_ctx,
ngx_http_mytest_commands,
NGX_HTTP_MODULE,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NGX_MODULE_V1_PADDING
}; static char *
ngx_http_mytest(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{
ngx_http_core_loc_conf_t *clcf; clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module); clcf->handler = ngx_http_mytest_handler; return NGX_CONF_OK;
} static ngx_int_t ngx_http_mytest_handler(ngx_http_request_t *r)
{
if (!(r->method & (NGX_HTTP_GET | NGX_HTTP_HEAD))) {
return NGX_HTTP_NOT_ALLOWED;
} ngx_int_t rc = ngx_http_discard_request_body(r);
if (rc != NGX_OK) {
return rc;
} ngx_str_t type = ngx_string("text/plain");
ngx_str_t response = ngx_string("miao miao");
r->headers_out.status = NGX_HTTP_OK;
r->headers_out.content_length_n = response.len;
r->headers_out.content_type = type; rc = ngx_http_send_header(r);
if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) {
return rc;
} ngx_buf_t *b;
b = ngx_create_temp_buf(r->pool, response.len);
if (b == NULL) {
return NGX_HTTP_INTERNAL_SERVER_ERROR;
} ngx_memcpy(b->pos, response.data, response.len);
b->last = b->pos + response.len;
b->last_buf = ; ngx_chain_t out;
out.buf = b;
out.next = NULL; return ngx_http_output_filter(r, &out);
}

  a.  ngx_command_t

  定义服务器配置文件(安装目录/conf/nginx.conf)对应的配置项,并定义了配置项对应的回调函数,在此为ngx_http_mytest。

  b.  ngx_http_module_t

  模块上下文,每个null分别对应着一个函数指针

 ngx_http_module_t ngx_http_mytest_module_ctx = {

        NULL,                                  /* preconfiguration */
NULL,           /* postconfiguration */ NULL, /* create main configuration */
NULL, /* init main configuration */ NULL, /* create server configuration */
NULL, /* merge server configuration */ NULL, /* create location configuration */
NULL /* merge location configuration */
};

  c.  ngx_module_t

  对于开发一个模块来说,需要定义一个ngx_module_t类型的变量来说明这个模块本身的信息,从某种意义上来说,这是这个模块最重要的一个信息,它告诉了nginx这个模块的一些信息,上面定义的配置信息,还  有模块上下文信息,都是通过这个结构来告诉nginx系统的,也就是加载模块的上层代码,都需要通过定义的这个结构,来获取这些信息。

ngx_module_t ngx_http_mytest_module = {
  NGX_MODULE_V1,    /*  #define NGX_MODULE_V1 0, 0, 0, 0,NGX_DSO_ABI_COMPATIBILITY, NGX_NUMBER_MAJOR, NGX_NUMBER_MINOR    */
  &ngx_http_mytest_module_ctx,  /* module context */
  ngx_http_mytest_commands,  /* module directives */
  NGX_HTTP_MODULE,   /* module type */
  NULL,   /* init master */
  NULL,   /* init module */
  NULL,   /* init process */
  NULL, /* init thread */
  NULL, /* exit thread */
  NULL, /* exit process */
  NULL, /* exit master */
  NGX_MODULE_V1_PADDING /*    #define NGX_MODULE_V1_PADDING 0, 0, 0, 0, 0, 0, 0, 0    */
};

3.  编译安装新模块

  Nginx提供了一种简单的方式将第三方的模块编译到Nginx中。首先把源代码文件全部放到一个目录下,同时在该目录中编写一个文件用于通知Nginx如何编译本模块,这个文件名必须为config。然后,在configure脚本执行时加入参数--add-module=PATH(新模块源代码以及config文件存放目录),就可以在执行政策编译安装流程时完成Nginx编译工作。Nginx添加新模块要求整个服务器重新编译应该是为了性能的需求。

  config文件格式
  config文件其实是一个可执行的Shell脚本,如果只想开发一个HTTP模块,需要定义三个变量:
  (1)ngx_adon_name。
  仅在configure执行时使用,一般设置为模块名称。
  (2)HTTP_MODULES。
  保存所有的HTTP模块名称。每个模块间由空格相连。在重新设置这个变量时,不要直接覆盖,因此要如下设置:
  "$HTTP_MODULES ngx_http_mytest_module"
  (3)NGX_ADDON_SRCS。
  用于指定新模块的源代码,多个待编译的源代码之间可以用空格相连。
  注意,在设置这个变量时可以使用$ngx_addon_dir变量,它等价于configure执行时--add-module=PATH的PATH参数。

ngx_addon_name=ngx_http_mytest_module
HTTP_MODULES="$HTTP_MODULES ngx_http_mytest_module"
NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ngx_addon_dir/ngx_http_mytest_module.c"

  编译安装新模块的命令如下:

./configure --prefix=/usr/local/nginx(指定安装部署后的根目录) --add-module=/home/nginx(新模块存放目录)
make
sudo make install

4.  推荐内容

Nginx开发从入门到精通  http://tengine.taobao.org/book/index.html

nginx(一)的更多相关文章

  1. accept_mutex与性能的关系 (nginx)

    注:运行环境CentOS 6+   背景      在对启动了20个worker的nginx进行压力测试的时候发现:如果把配置文件中event配置块中的accept_mutex开关打开(1.11.3版 ...

  2. nginx配置反向代理或跳转出现400问题处理记录

    午休完上班后,同事说测试站点访问接口出现400 Bad Request  Request Header Or Cookie Too Large提示,心想还好是测试服务器出现问题,影响不大,不过也赶紧上 ...

  3. 【大型网站技术实践】初级篇:借助Nginx搭建反向代理服务器

    一.反向代理:Web服务器的“经纪人” 1.1 反向代理初印象 反向代理(Reverse Proxy)方式是指以代理服务器来接受internet上的连接请求,然后将请求转发给内部网络上的服务器,并将从 ...

  4. Docker笔记一:基于Docker容器构建并运行 nginx + php + mysql ( mariadb ) 服务环境

    首先为什么要自己编写Dockerfile来构建 nginx.php.mariadb这三个镜像呢?一是希望更深入了解Dockerfile的使用,也就能初步了解docker镜像是如何被构建的:二是希望将来 ...

  5. nginx+php的使用

    原文来自:windows下配置nginx+php环境 按照他的步骤走,亲测可用! 但是这里他后面说的根目录可能有些人有点懵. 其实在设置的时候就设置了: 网站根目录就是www这个目录,如果没创建请自行 ...

  6. nginx的使用

    1.nginx的下载 解压后文件目录: 2.nginx的常用命令 nginx -s stop 强制关闭  nginx -s quit 安全关闭  nginx -s reload 改变配置文件的时候,重 ...

  7. nginx+iis+redis+Task.MainForm构建分布式架构 之 (redis存储分布式共享的session及共享session运作流程)

    本次要分享的是利用windows+nginx+iis+redis+Task.MainForm组建分布式架构,上一篇分享文章制作是在windows上使用的nginx,一般正式发布的时候是在linux来配 ...

  8. windows+nginx+iis+redis+Task.MainForm构建分布式架构 之 (nginx+iis构建服务集群)

    本次要分享的是利用windows+nginx+iis+redis+Task.MainForm组建分布式架构,由标题就能看出此内容不是一篇分享文章能说完的,所以我打算分几篇分享文章来讲解,一步一步实现分 ...

  9. nginx源码分析之模块初始化

    在nginx启动过程中,模块的初始化是整个启动过程中的重要部分,而且了解了模块初始化的过程对应后面具体分析各个模块会有事半功倍的效果.在我看来,分析源码来了解模块的初始化是最直接不过的了,所以下面主要 ...

  10. Nginx如何处理一个请求

    看了下nginx的官方文档,其中nginx如何处理一个请求讲解的很好,现在贴出来分享下.Nginx首先选定由哪一个虚拟主机来处理请求.让我们从一个简单的配置(其中全部3个虚拟主机都在端口*:80上监听 ...

随机推荐

  1. 1006. Sign In and Sign Out (25)

    At the beginning of every day, the first person who signs in the computer room will unlock the door, ...

  2. django例子,question_text为中文时候报错

    问题描述 UnicodeEncodeError at /admin/polls/question/3/ 'ascii' codec can't encode characters in positio ...

  3. centos6.7 mutlipath install script

    #!/bin/bash if [ `id -g` != 0 ] ;then echo -e "\033[31m Please use root user\033[0m" exit ...

  4. phalcon: 多模块多表查找,多表sql

    那么多模块下,如何分页的,如果直接用->from(表名),报错找不到此类,此时要引用model类的全命名空间名称如下: $builder = $this->modelsManager-&g ...

  5. Linux基础知识

    1.url中不写端口号,默认就是80端口:本机是127.0.0.1或者localhost 2.用户管理 查看当前用户: id:可以查看当前用户:whoami:查看当前的用户:who:可以查看当前已经登 ...

  6. error CS0117: `UnityEditor.EditorUtility' does not contain a definition for `GetAssetPreview'

    have to replace: EditorUtility by AssetPreview

  7. python爬虫实战(一)——实时获取代理ip

    在爬虫学习的过程中,维护一个自己的代理池是非常重要的. 详情看代码: 1.运行环境 python3.x,需求库:bs4,requests 2.实时抓取西刺-国内高匿代理中前3页的代理ip(可根据需求自 ...

  8. Numpy Study 1

    Numpy 使用1 1.Numpy创建数组 import numpy as np 创建数组有以下方式: (1).arange numpy.arange([start, ]stop, [step, ]d ...

  9. centos 安装mongodb

    原文引用:http://blog.chinaunix.net/uid-24250828-id-3770298.html 官方网站: http://www.mongodb.org/ MongoDB 安裝 ...

  10. Socket.io和Redis写Realtime App 之express初试

    第一步:用npm下载express前端框架 注意事项:首先要确保已经安装了node.js和 npm 然后在项目中创建一个package.json文件,不能完全为空不写,至少要有两个大括号,不然怎么证明 ...