<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # 6.3 Nginx與FastCGI nginx 不能像apache那樣直接執行外部可執行程序,但nginx可以作為代理服務器,將請求轉發給后端服務器,這也是nginx的主要作用之一。其中nginx就支持FastCGI代理,接收客戶端的請求,然后將請求轉發給后端fastcgi進程。下面介紹如何使用C/C++編寫cgi/fastcgi,并部署到nginx中。 通過前面的介紹知道,fastcgi進程由FastCGI進程管理器管理,而不是nginx。這樣就需要一個FastCGI管理,管理我們編寫fastcgi程序。本文使用spawn-fcgi作為FastCGI進程管理器。 ## spawn-fcgi spawn-fcgi是一個通用的FastCGI進程管理器,簡單小巧,原先是屬于lighttpd的一部分,后來由于使用比較廣泛,所以就遷移出來作為獨立項目了。spawn-fcgi使用pre-fork 模型,功能主要是打開監聽端口,綁定地址,然后fork-and-exec創建我們編寫的fastcgi應用程序進程,退出完成工作。fastcgi應用程序初始化,然后進入死循環偵聽socket的連接請求。 ### 安裝 * 獲取spawn-fcgi編譯安裝包,在 http://redmine.lighttpd.net/projects/spawn-fcgi/wiki 上可以獲取當前最新的版本。 * 解壓縮spawn-fcgi-x.x.x.tar.gz包。 * 進入解壓縮目錄,執行./configure。 * make & make install 如果遇到以下錯誤: 如果遇到以下錯誤: ```bash ./autogen.sh: x: autoreconf: not found ``` 因為沒有安裝automake 工具,ubuntu用下面的命令安裝好就可以了: ```bash sudo apt-get install autoconf automake libtool 。 ``` spawn-fcgi的幫助信息可以通過man spawn-fcgi或spawn-fcgi –h獲得,下面是部分常用spawn-fcgi參數信息: ```bash f <fcgiapp> 指定調用FastCGI的進程的執行程序位置 -a <addr> 綁定到地址addr。 -p <port> 綁定到端口port。 -s <path> 綁定到unix domain socket -C <childs> 指定產生的FastCGI的進程數,默認為5。(僅用于PHP) -P <path> 指定產生的進程的PID文件路徑。 -F <childs> 指定產生的FastCGI的進程數(C的CGI用這個) -u和-g FastCGI使用什么身份(-u 用戶 -g 用戶組)運行, CentOS下可以使用apache用戶,其他的根據情況配置,如nobody、www-data等。 ``` ### 編寫fastgci應用程序 使用C/C++編寫fastcgi應用程序,可以使用FastCGI軟件開發套件或者其它開發框架,如fastcgi++。 本文使用FastCGI軟件開發套件——fcgi http://www.filewatcher.com/d/Gentoo/distfiles/Other/fcgi-2.4.1-SNAP-0910052249.tar.gz.614929.html 通過此套件可以輕松編寫fastcgi應用程序,安裝fcgi: ```bash ./configue make ``` 如果編譯出現類似以下錯誤: ```bash cgio.cpp: In destructor 'virtual fcgi_streambuf::~fcgi_streambuf()': fcgio.cpp:50: error: 'EOF' was not declared in this scope fcgio.cpp: In member function 'virtual int fcgi_streambuf::overflow(int)': fcgio.cpp:70: error: 'EOF' was not declared in this scope fcgio.cpp:75: error: 'EOF' was not declared in this scope fcgio.cpp: In member function 'virtual int fcgi_streambuf::sync()': fcgio.cpp:86: error: 'EOF' was not declared in this scope fcgio.cpp:87: error: 'EOF' was not declared in this scope fcgio.cpp: In member function 'virtual int fcgi_streambuf::underflow()': fcgio.cpp:113: error: 'EOF' was not declared in this scope make[2]: *** [fcgio.lo] Error 1 make[2]: Leaving directory `/root/downloads/fcgi-2.4.1-SNAP-0910052249/libfcgi' make[1]: *** [all-recursive] Error 1 make[1]: Leaving directory `/root/downloads/fcgi-2.4.1-SNAP-0910052249' make: *** [all] Error 2 ``` 解決辦法:在include/fcgio.h文件中加上** ```cpp #include <cstdio> ``` ,然后再次編譯 ```bash sudo make install ``` 編寫一個fcgi簡單的應用程序: ```cpp #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include "fcgi_stdio.h" int main(int argc, char *argv[]) { int count = 0; while (FCGI_Accept() >= 0) { printf("Content-type: text/html\r\n"); printf("\r\n"); printf("<title>Fast CGI Hello!</title>"); printf("<h1>Fast CGI Hello!</h1>"); printf("Request number %d running on host <i>%s</i>\n", ++count, getenv("SERVER_NAME")); } return 0; } ``` 編譯: ```bash gcc fcgi_demo.c -o demo -lfcgi ``` 這個就是其中一個針對client一個http請求的業務服務應用程序。 需要在后臺跑起來,并且讓spawn負責管理。 ```bash spawn-fcgi -a 127.0.0.1 -p 8081 -f ./demo ``` ### 有關nginx的fcgi的配置 ```bash server { listen 80; server_name localhost; #access_log logs/host.access.log main; location / { #proxy #proxy_pass http://backup.com; root html; index index.html index.htm; } #監聽用戶的demo.cgi請求,通過fastcgi_pass 交給本地8081端口處理 #此時spwan-cgi已經將8081端口交給之前我們寫好的demo進程處理 location /demo { fastcgi_pass 127.0.0.1:8081; fastcgi_index demo.cgi; include fastcgi.conf; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } ``` 這樣nginx收到http://localhost/demo請求時,會匹配到location /demo 塊,將請求傳到后端的fastcgi應用程序處理。 ![](https://img.kancloud.cn/ab/16/ab169ae269156b3e2ac5ddaa8de078a9_455x151.png)
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看