dongdhui 2017-01-16 12:15 采纳率: 0%
浏览 777

nginx helloworld 404错误

nginx 编写helloworld模块,报错,404 Not Found
配置文件:
location /test {
mytest;
}

config文件:

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"
CORE_LIBS="$CORE_LIBS -lpcre"

c码如下:
1 #include

2 #include

3 #include

4

5 static ngx_int_t ngx_http_mytest_handler(ngx_http_request_t *r);

6 static char *

7 ngx_http_mytest(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);

8 //处理配置项

9 static ngx_command_t ngx_http_mytest_commands[] = {

10 {

11 ngx_string("mytest"),

13 ngx_http_mytest,

14 NGX_HTTP_LOC_CONF_OFFSET,

15 0,

16 NULL

17 },

18 ngx_null_command

19 };

20 //模块上下文

21 static ngx_http_module_t ngx_http_mytest_module_ctx = {

22 NULL,

23 NULL,

24 NULL,

25 NULL,

26 NULL,

27 NULL,

28 NULL,

29 NULL

30 };

31 //新模块定义

32 ngx_module_t ngx_http_mytest_module = {

33 NGX_MODULE_V1,

34 &ngx_http_mytest_module_ctx,

35 ngx_http_mytest_commands,

36 NGX_HTTP_MODULE,

37 NULL,

38 NULL,

39 NULL,

40 NULL,

41 NULL,

42 NULL,

43 NULL,

44 NGX_MODULE_V1_PADDING

45 };

46

47 //配置项对应的回调函数

48 static char *

49 ngx_http_mytest(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)

50 {

51 ngx_http_core_loc_conf_t *clcf;

52 clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);

53 clcf->handler = ngx_http_mytest_handler;

54 return NGX_CONF_OK;

55 }

56

57 //实际完成处理的回调函数

58 static ngx_int_t ngx_http_mytest_handler(ngx_http_request_t *r)

59 {

60 if (!(r->method & (NGX_HTTP_GET | NGX_HTTP_HEAD))) {

61 return NGX_HTTP_NOT_ALLOWED;

62 }

63

64 ngx_int_t rc = ngx_http_discard_request_body(r);

65 if (rc != NGX_OK) {

66 return rc;

67 }

68

69 ngx_str_t type = ngx_string("text/plain");

70 ngx_str_t response = ngx_string("Hello World");

71 r->headers_out.status = NGX_HTTP_OK;

72 r->headers_out.content_length_n = response.len;

73 r->headers_out.content_type = type;

74

75 rc = ngx_http_send_header(r);

76 if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) {

77 return rc;

78 }

79

80 ngx_buf_t *b;

81 b = ngx_create_temp_buf(r->pool, response.len);

82 if (b == NULL) {

83 return NGX_HTTP_INTERNAL_SERVER_ERROR;

84 }

85

86 ngx_memcpy(b->pos, response.data, response.len);

87 b->last = b->pos + response.len;

88 b->last_buf = 1;

89

90 ngx_chain_t out;

91 out.buf = b;

92 out.next = NULL;

93

94 return ngx_http_output_filter(r, &out);

95 }

执行结果:
[root@localhost objs]# ps -ef | grep nginx
root 29043 1 0 19:50 ? 00:00:00 nginx: master process ./nginx
nobody 29044 29043 0 19:50 ? 00:00:00 nginx: worker process
root 29188 18558 0 19:58 pts/0 00:00:00 grep --color=auto nginx
[root@localhost objs]# curl http://localhost/test

404 Not Found

404 Not Found



nginx/1.10.2

  • 写回答

1条回答

  • devmiao 2017-01-29 18:03
    关注
    评论

报告相同问题?

悬赏问题

  • ¥15 fesafe材料库问题
  • ¥35 beats蓝牙耳机怎么查看日志
  • ¥15 Fluent齿轮搅油
  • ¥15 八爪鱼爬数据为什么自己停了
  • ¥15 交替优化波束形成和ris反射角使保密速率最大化
  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统