Nginx 输出处理过程

       在HTTP处理过程中我们会不断的见到若干函数先说下
      static ngx_int_t ngx_http_echo_handler(ngx_http_request_t *r)
{
       ngx_int_t rc;
       ngx_buf_t *b; 
       //ngx_chain_t是关于ngx_buf_t的一个链表
       ngx_chain_t out; 
       ngx_http_echo_loc_conf_t *elcf; //读取我们写在nginx.conf文件中的配置信息
       elcf = ngx_http_get_module_loc_conf(r, ngx_http_echo_module);
       if(!(r->method &  (NGX_HTTP_HEAD|NGX_HTTP_GET|NGX_HTTP_POST))) 
       { 
         return NGX_HTTP_NOT_ALLOWED; 
       }
      //headers_out我们可以认为是响应头部的处理
       r->headers_out.content_type.len = sizeof("text/html") - 1;
       r->headers_out.content_type.data = (u_char *) "text/html";
       r->headers_out.status = NGX_HTTP_OK; 
       r->headers_out.content_length_n = elcf->ed.len; 
       if(r->method == NGX_HTTP_HEAD) { 
           rc = ngx_http_send_header(r); 
           if(rc != NGX_OK) { 
               return rc; 
         } 
      } 
       b = ngx_pcalloc(r->pool, sizeof(ngx_buf_t)); 
       if(b == NULL) 
        {
             ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "Failed to allocate response buffer."); 
             return NGX_HTTP_INTERNAL_SERVER_ERROR; 
        } 
       //设置ngx_chain_t中的ngx_buf_t链表,next为NULL则说明该链表结束
       out.buf = b; 
       out.next = NULL; 
       //设置缓存内容
       b->pos = elcf->ed.data; 
       b->last = elcf->ed.data + (elcf->ed.len);
       b->memory = 1;
       //当前ngx_buf_t已经是ngx_chain_t链表中的最后一项 
       b->last_buf = 1; 
       rc = ngx_http_send_header(r); 
       if(rc != NGX_OK) 
       {
          return rc; 
        } 
     return ngx_http_output_filter(r, &out);
} 

就像我们代码中出现的那样,现在解析两个函数,其一ngx_http_send_request,代码如下

 ngx_http_send_header(ngx_http_request_t *r)
 {
     if (r->header_sent) {
         ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0,
                       "header already sent");
         return NGX_ERROR;
     }
 
     if (r->err_status) {
         r->headers_out.status = r->err_status;
         r->headers_out.status_line.len = 0;
     }
 
     return ngx_http_top_header_filter(r);
 }
这段代码实现的并不直接是发送功能,而是根据过滤模块的ngx_http_top_header_filter链表将请求头部进行处理,接下来就是ngx_http_output_filter函数了,

该函数定义如下:

 ngx_int_t
 ngx_http_output_filter(ngx_http_request_t *r, ngx_chain_t *in)
 {
     ngx_int_t          rc;
     ngx_connection_t  *c;
 
     c = r->connection;
 
     ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0,
                    "http output filter \"%V?%V\"", &r->uri, &r->args);
 
     rc = ngx_http_top_body_filter(r, in);
 
     if (rc == NGX_ERROR) {
         /* NGX_ERROR may be returned by any filter */
         c->error = 1;
     }
 
     return rc;
}
可以看到ngx_http_output_filter最终调用的是ngx_http_top_body_filter函数,这里面用到了两个参数,一个是ngx_http_request_t请求,一个是ngx_chain_t链表,其实就是将传递的ngx_chain_t添加到请求的out(也是ngx_chain_t数据类型的)的尾部,在进行输出的时候,将其内容添加到尾部。这样一个页面就输出了


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
### 回答1: 要在 Nginx输出指定的 location,可以使用以下配置: ``` location /example { return 200 "This is the example location"; } ``` 这将返回一个 HTTP 200 响应,其正文为 "This is the example location"。可以将 /example 替换为您想要输出的 location。 如果您想要将请求重定向到该 location,可以使用以下配置: ``` location /old-url { return 301 /new-url; } ``` 这将将所有访问 /old-url 的请求重定向到 /new-url。 ### 回答2: 在使用Nginx的过程中,可以通过配置文件来指定输出指定的location。具体的操作步骤如下: 1. 打开Nginx的配置文件,通常是`/etc/nginx/nginx.conf`。 2. 在配置文件中找到指定的location所在的server块。例如,如果要指定输出的是`www.example.com`域名的`/test`路径,可以在配置文件中找到对应的server块。 3. 在找到的server块内,使用`location`关键字来定义指定的location。例如: ``` server { listen 80; server_name www.example.com; location /test { # 在这里进行具体的配置 } } ``` 4. 在`location`块内,可以根据具体需求进行配置。常见的配置选项包括`root`、`proxy_pass`等。例如,可以使用`root`配置指定location的根目录,如: ``` location /test { root /var/www/test; } ``` 这样配置后,访问`www.example.com/test`将会输出`/var/www/test`目录下的文件。 5. 完成配置后,保存并退出配置文件。 6. 重启Nginx服务,使配置生效。在终端中运行命令`sudo service nginx restart`。 通过上述步骤,就可以在Nginx中进行指定的location输出的配置。根据实际需求,可以灵活修改Nginx配置文件来满足不同的要求。 ### 回答3: 在 Nginx 中,可以使用 location 指令来定义不同 URI(Uniform Resource Identifier)的访问规则和处理方式。当客户端请求的 URI 与定义的 location 匹配时,Nginx 会根据相应的配置进行处理和响应。 要输出指定的 location,首先需要在 Nginx 的配置文件中定义该 location。可以使用以下语法: ``` location /example { # 指定 location 的配置 } ``` 其中,`/example` 是我们想要指定的 URI。在上述 location 中,我们可以编写相关指令来处理该 URI 的请求。例如,可以使用 `proxy_pass` 指令将请求代理到其他服务器,也可以使用 `root` 指令指定访问该 URI 时的文件根目录等。 当客户端请求的 URI 与定义的 `/example` location 匹配时,Nginx 会按照我们在该 location 中配置的指令进行处理。如果有对应的文件,则会输出该文件;如果有代理指令,则会代理请求;如果有其他指令,则会按指令进行相应的处理。 除了匹配具体的 URI,还可以使用正则表达式来指定 location。例如,可以使用以下语法: ``` location ~ ^/example\d+$ { # 指定 location 的配置 } ``` 上述正则表达式表示匹配以 `/example` 开头,后面跟一个或多个数字的 URI。当客户端请求的 URI 符合该正则表达式时,Nginx 会使用该 location 的配置进行处理。 总之,要输出指定的 location,只需在 Nginx 的配置文件中定义该 location,并编写相应的指令来处理该 location 的请求即可。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

世纪殇

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值