nginx+fastcgi 实践

  一、简介

   项目上需要用到web处理用户提交数据,考虑到板上资源足够,遂采用nginx+fastcgi。本文记录下过程。

 

  二、源文件下载

    Nginx 采用 nginx-1.16.0,

    Fastcgi 采用 fcgi-2.4.0。

 

  三、Nginx 编译

      Nginx 为了尽量缩小占用空间, 使用下面的配置

     

./configure --prefix=/dev/shm/nuc970/server --user=root --group=root --crossbuild=arm-linux --with-cc=arm-linux-gcc --with-cpp=arm-linux-g++ \

     --without-http_gzip_module \

     --without-http_ssi_module \

     --without-http_userid_module \

     --without-http_access_module \

     --without-http_auth_basic_module \

     --without-http_mirror_module \

     --without-http_geo_module \

     --without-http_map_module \

     --without-http_split_clients_module \

     --without-http_referer_module \

     --without-http_rewrite_module \

     --without-http_uwsgi_module \

     --without-http_scgi_module \

     --without-http_grpc_module \

     --without-http_memcached_module \

     --without-http_limit_conn_module \

     --without-http_limit_req_module \

     --without-http_empty_gif_module \

     --without-http_browser_module \

     --without-http_upstream_hash_module \

     --without-http_upstream_ip_hash_module \

     --without-http_upstream_least_conn_module \

     --without-http_upstream_keepalive_module \

     --without-stream_upstream_zone_module \

     --without-http_upstream_zone_module \

     --without-mail_pop3_module \

     --without-mail_imap_module \

     --without-mail_smtp_module \

     --without-stream_limit_conn_module \

     --without-stream_access_module \

     --without-stream_geo_module \

     --without-stream_map_module \

     --without-stream_split_clients_module \

     --without-stream_return_module \

     --without-stream_upstream_hash_module \

     --without-stream_upstream_least_conn_module \

     --without-stream_upstream_zone_module \

     --without-pcre

过程中遇到的一些报错,如下

  


==================

踩坑四,make的时候报错


    src/core/ngx_rwlock.c:125:2: error: #error ngx_atomic_cmp_set() is not defined!


报错的地方,nginx的源码是这样的


    #if (NGX_HTTP_UPSTREAM_ZONE || NGX_STREAM_UPSTREAM_ZONE)

    

    #error ngx_atomic_cmp_set() is not defined!

    

    #endif


解决办法,在configure的时候加上--without-http_upstream_zone_module

==================

 修改 ngx_auto_config.h

 对应 宏定义


---#define NGX_HAVE_ACCEPT4  1


---#define NGX_HAVE_IPV6_RECVPKTINFO  1




---#define NGX_HAVE_PREAD  1

---#define NGX_HAVE_PWRITE  1


+++#ifndef NGX_SYS_NERR

+++#define NGX_SYS_NERR  132

+++#endif


+++#ifndef NGX_HAVE_SYSVSHM

+++#define NGX_HAVE_SYSVSHM 1

+++#endif



make install

  四.  Fastcgi 编译

./configure --prefix=/mnt/hgfs/vm_shared/web/fcgi --host=arm CC=arm-linux-gcc CXX=arm-linux-g++

 

   五. Nginx 配置修改

          Nginx 需要添加配置

        

location = /post_form {

            #root           html;

            fastcgi_pass   127.0.0.1:9080;

            fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;

            include        fastcgi_params;

        }

    六、例程

// 1. 启动一个tcp server ,并监听 9080 端口
...
// 2. 处理程序

int fcgi_fun (int socket)

{

    int count = 0;


    uint8_t buffer_r[MAX_RECV_LEN];

    uint8_t buffer_w[MAX_RECV_LEN];

    FCGX_Request request;


    FCGX_Init();

    FCGX_InitRequest(&request, socket, 0);


    while(1)

    {

       

        while (FCGX_Accept_r(&request) == 0)

        {

               char* req_uri = FCGX_GetParam("REQUEST_URI", request.envp);

               char* script_name = FCGX_GetParam("SCRIPT_NAME", request.envp);

               char *contentLength = FCGX_GetParam("CONTENT_LENGTH", request.envp);

                int len, reboot_flag=0;


               memset(buffer_w, 0, MAX_RECV_LEN);


               if (contentLength != NULL)

               {

                    len = strtol(contentLength, NULL, 10);

                }

                else

                {

                    len = 0;

                }

              

               if(strcmp("/post_form", req_uri) == 0)

               {

                      if ( strcmp("GET", FCGX_GetParam("REQUEST_METHOD", request.envp)) == 0 ) 

                      { 

                             char* get_data = FCGX_GetParam("QUERY_STRING", request.envp);

                             memset( buffer_r, 0, 1024 );

                             memcpy( buffer_r, get_data, strlen(get_data) );

                      } 

                      else 

                      { 

                             memset( buffer_r, 0, len+1 );


                             FCGX_GetStr(buffer_r, len, request.in);

                      }


               }

              

               FCGX_FPrintF(request.out, "Content-type: application/json\r\n"

                      "Access-Control-Allow-Origin: *\r\n"

                   "\r\n" "%s\n", buffer_w);


        }


    }


    return 0;

}

 

你可能感兴趣的:(linux,嵌入式,fastcgi,fcgi)