CppCMS和Nginx协同工作

为什么需要Nginx

CppCMS自己已经支持Http访问,为什么还需要Nginx.一些原因是我们需要动态负载平衡,还有原因是安全问题。下面的文字来自于CppCMS站点。
CppCMS provides basic implementation of HTTP protocol and thus it can run behind reverse HTTP Proxy. But this option is not recommended for use in production environment unless HTTP protocol is fully sanitized by the proxy itself, otherwise you may expose yourself to various security issues.

Nginx开启FastCGI

在nginx.conf文件的server一节添加如下配置,请注意^/hello(.*)$代表用CppCMS构建的Web站点的URI的上下文路径。而fastcgi_pass 192.168.30.17:8081 则是指Nginx要和位于30.17的CppCMS站点通过FastCGI协议通信。这里其实可以指定多个.

location ~ ^/kaimei.*$ {

    fastcgi_pass localhost:8080;

    fastcgi_split_path_info ^(/kaimei)(.*)$;
    fastcgi_param SCRIPT_FILENAME  $fastcgi_script_name;
    fastcgi_param PATH_INFO        $fastcgi_path_info;

    fastcgi_param  SCRIPT_NAME     /kaimei;
    fastcgi_param  QUERY_STRING    $query_string;
    fastcgi_param  REQUEST_METHOD  $request_method;
    fastcgi_param  CONTENT_TYPE    $content_type;
    fastcgi_param  CONTENT_LENGTH  $content_length;

    fastcgi_param  REQUEST_URI     $request_uri;
    fastcgi_param  DOCUMENT_URI    $document_uri;
    fastcgi_param  DOCUMENT_ROOT   $document_root;
    fastcgi_param  SERVER_PROTOCOL $server_protocol;

    fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
    fastcgi_param  SERVER_SOFTWARE    nginx;

    fastcgi_param  REMOTE_ADDR        $remote_addr;
    fastcgi_param  REMOTE_PORT        $remote_port;
    fastcgi_param  SERVER_ADDR        $server_addr;
    fastcgi_param  SERVER_PORT        $server_port;
    fastcgi_param  SERVER_NAME        $server_name;
}

简单说明:

location ~ 后面是正则表达式,符合/kaimei开头的URL都会使用location这段配置。

其他可以看nginx文档:http://nginx.org/en/docs/http/ngx_http_fastcgi_module.html#fastcgi_split_path_info

这段配置可以单独存放在文件中,比如my.conf

然后在nginx.conf中 用include my.conf; 引入。

CppCMS配置

现在来配置30.17上的CppCMS服务。主要是config.js文件的内容:
"service" : {
"api" : "fastcgi",
"ip" : "192.168.30.17",
"port" : 8081
}

测试

现在启动CppCMS服务和Nginx服务。然后测试通过。

你可能感兴趣的:(nginx)