国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

首頁 運維 nginx Linux下如何用Nginx作Perl程序服務(wù)器及其中Perl模塊

Linux下如何用Nginx作Perl程序服務(wù)器及其中Perl模塊

May 16, 2023 pm 11:25 PM
linux nginx perl

perl + fastcgi + nginx搭建

nginx + fastcgi是php下最流行的一套環(huán)境了,那perl會不會也有fastcgi呢,當(dāng)然有,今天來搭建下nginx下perl的fastcgi.性能方面也不亞于php,但是現(xiàn)在web程序php的流行程度perl無法比擬了,性能再好也枉然,但是部分小功能可以考慮使用perl的fastcgi來搞定.進入正題.
1. 準(zhǔn)備軟件環(huán)境:

nginx
perl:系統(tǒng)自帶
fastcgi

1.2 perl安裝
一般linux都有自帶perl,可以不用安裝,如果確實沒有,請執(zhí)行:

# yum install perl

1.3 perl-fastcgi安裝

# cd /usr/local/src
# wget http://www.cpan.org/modules/by-module/fcgi/fcgi-0.74.tar.gz
# tar -xzvf fcgi-0.74.tar.gz
# cd fcgi-0.74
# perl makefile.pl 
# make
# make install

2. nginx虛擬主機配置

server {
 
  listen  80;
  server_name test.jb51.net;
  #access_log /data/logs/nginx/test.jb51.net.access.log main;
 
  index index.html index.php index.html;
  root /data/site/test.jb51.net;
 
  location / 
  {
 
  }
 
  location ~ \.pl$ 
  {
   include fastcgi_params;
   fastcgi_pass 127.0.0.1:8999;
   #fastcgi_pass unix:/var/run/jb51.net.perl.sock;
   fastcgi_index index.pl;
  }
}

如果想把tcp/ip方式改為socket方式,可以修改fastcgi-wrapper.pl.

$socket = fcgi::opensocket( "127.0.0.1:8999", 10 ); #use ip sockets

改為

$socket = fcgi::opensocket( "/var/run/jb51.net.perl.sock", 10 ); #use ip sockets

3. 配置腳本

3.1 fastcgi監(jiān)聽腳本
文件路徑:/usr/bin/fastcgi-wrapper.pl

#!/usr/bin/perl
 
use fcgi;
use socket;
use posix qw(setsid);
 
require 'syscall.ph';
 
&daemonize;
 
#this keeps the program alive or something after exec'ing perl scripts
end() { } begin() { }
*core::global::exit = sub { die "fakeexit\nrc=".shift()."\n"; };
eval q{exit};
if ($@) {
 exit unless $@ =~ /^fakeexit/;
};
 
&main;
 
sub daemonize() {
 chdir '/'     or die "can't chdir to /: $!";
 defined(my $pid = fork) or die "can't fork: $!";
 exit if $pid;
 setsid     or die "can't start a new session: $!";
 umask 0;
}
 
sub main {
  $socket = fcgi::opensocket( "127.0.0.1:8999", 10 ); #use ip sockets
  $request = fcgi::request( \*stdin, \*stdout, \*stderr, \%req_params, $socket );
  if ($request) { request_loop()};
   fcgi::closesocket( $socket );
}
 
sub request_loop {
  while( $request->accept() >= 0 ) {
 
   #processing any stdin input from webserver (for cgi-post actions)
   $stdin_passthrough ='';
   $req_len = 0 + $req_params{'content_length'};
   if (($req_params{'request_method'} eq 'post') && ($req_len != 0) ){
    my $bytes_read = 0;
    while ($bytes_read < $req_len) {
      my $data = '';
      my $bytes = read(stdin, $data, ($req_len - $bytes_read));
      last if ($bytes == 0 || !defined($bytes));
      $stdin_passthrough .= $data;
      $bytes_read += $bytes;
    }
   }
 
   #running the cgi app
   if ( (-x $req_params{script_filename}) && #can i execute this?
     (-s $req_params{script_filename}) && #is this file empty?
     (-r $req_params{script_filename})  #can i read this file?
   ){
  pipe(child_rd, parent_wr);
  my $pid = open(kid_to_read, "-|");
  unless(defined($pid)) {
   print("content-type: text/plain\r\n\r\n");
      print "error: cgi app returned no output - ";
      print "executing $req_params{script_filename} failed !\n";
   next;
  }
  if ($pid > 0) {
   close(child_rd);
   print parent_wr $stdin_passthrough;
   close(parent_wr);
 
   while(my $s = ) { print $s; }
   close kid_to_read;
   waitpid($pid, 0);
  } else {
     foreach $key ( keys %req_params){
      $env{$key} = $req_params{$key};
     }
     # cd to the script's local directory
     if ($req_params{script_filename} =~ /^(.*)\/[^\/]+$/) {
       chdir $1;
     }
 
   close(parent_wr);
   close(stdin);
   #fcntl(child_rd, f_dupfd, 0);
   syscall(&sys_dup2, fileno(child_rd), 0);
   #open(stdin, "<&child_rd");
   exec($req_params{script_filename});
   die("exec failed");
  }
   }
   else {
    print("content-type: text/plain\r\n\r\n");
    print "error: no such cgi app - $req_params{script_filename} may not ";
    print "exist or is not executable by this process.\n";
   }
 
  }
}

3.2 fastcgi自啟動服務(wù)腳本:

文件路徑:/etc/rc.d/init.d/perl-fastcgi

文件路徑:/etc/rc.d/init.d/perl-fastcgi

#!/bin/sh
#
# nginx – this script starts and stops the nginx daemon
#
# chkconfig: - 85 15
# description: nginx is an http(s) server, http(s) reverse \
# proxy and imap/pop3 proxy server
# processname: nginx
# config: /opt/nginx/conf/nginx.conf
# pidfile: /opt/nginx/logs/nginx.pid
 
# source function library.
. /etc/rc.d/init.d/functions
 
# source networking configuration.
. /etc/sysconfig/network
 
# check that networking is up.
[ "$networking" = "no" ] && exit 0
 
perlfastcgi="/usr/bin/fastcgi-wrapper.pl"
prog=$(basename perl)
 
lockfile=/var/lock/subsys/perl-fastcgi
 
start() {
 [ -x $perlfastcgi ] || exit 5
 echo -n $"starting $prog: "
 daemon $perlfastcgi
 retval=$?
 echo
 [ $retval -eq 0 ] && touch $lockfile
 return $retval
}
 
stop() {
 echo -n $"stopping $prog: "
 killproc $prog -quit
 retval=$?
 echo
 [ $retval -eq 0 ] && rm -f $lockfile
 return $retval
}
 
restart() {
 stop
 start
}
 
reload() {
 echo -n $”reloading $prog: ”
 killproc $nginx -hup
 retval=$?
 echo
}
 
force_reload() {
 restart
}
rh_status() {
 status $prog
}
 
rh_status_q() {
 rh_status >/dev/null 2>&1
}
 
case "$1" in
 start)
  rh_status_q && exit 0
  $1
  ;;
 stop)
  rh_status_q || exit 0
  $1
  ;;
 restart)
  $1
  ;;
 reload)
  rh_status_q || exit 7
  $1
  ;;
 force-reload)
  force_reload
  ;;
 status)
  rh_status
  ;;
 condrestart|try-restart)
  rh_status_q || exit 0
  ;;
 *)
  echo $"usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload}"
  exit 2
 esac

3.3 設(shè)置腳本權(quán)限

# chmod a+x /usr/bin/fastcgi-wrapper.pl
# chmod a+x /etc/rc.d/init.d/perl-fastcgi


4. fastcgi測試
4.1 啟動nginx與fastcgi

# /usr/local/nginx-1.4.2/sbin/nginx
# /etc/init.d/perl-fastcgi start

4.2 perl測試文件:
文件路徑/data/site/test.jb51.net/test.pl

#!/usr/bin/perl
 
print "content-type:text/html\n\n";
print <<endofhtml;
<html><head><title>perl environment variables</title></head>
<body>
<h1>perl environment variables</h1>
endofhtml
 
foreach $key (sort(keys %env)) {
 print "$key = $env{$key}<br>\n";
}
 
print "</body></html>";

5. 訪問測試

5.1 訪問
http://http:test.jb51.net/test.pl,出現(xiàn)內(nèi)容表示ok.

6. 簡單壓力測試:
6.1 使用tcp/ip方式

ab -n 1000 -c 10 http://test.jb51.net/test.pl

他是在是太慢了,只好用10個并發(fā),共計100個請求來測試.

Linux下如何用Nginx作Perl程序服務(wù)器及其中Perl模塊

6.2 使用socket方式:

ab -n 100000 -c 500 http://test.jb51.net/test.pl

Linux下如何用Nginx作Perl程序服務(wù)器及其中Perl模塊

很奇怪,使用tcp/ip方式,每秒就140多個請求,而使用socket方式卻有5800個請求/秒。差距不是一般的大。順便測試了一下php的fastcgi,大概請求在3000(tcp/ip方式),4800(socket方式)。

perl模塊的使用
如果對于一個絕大部分內(nèi)容是靜態(tài)的網(wǎng)站,只有極少數(shù)的地方需要動態(tài)顯示,碰巧你又了解一點perl知識,那么nginx + perl的結(jié)合就能很好解決問題。要想nginx支持perl腳本,在編譯nginx時候需要如下參數(shù):

./configure --with-http_perl_module

如果make時候出現(xiàn)如下類似錯誤:

can&#39;t locate extutils/embed.pm in @inc (@inc contains: /usr/lib/perl5/5.10.0/i386-linux-thread-multi /usr/lib/perl5/5.10.0 /usr/local/lib/perl5/site_perl/5.10.0/i386-linux-thread-multi /usr/local/lib/perl5/site_perl/5.10.0 /usr/lib/perl5/vendor_perl/5.10.0/i386-linux-thread-multi /usr/lib/perl5/vendor_perl/5.10.0 /usr/lib/perl5/vendor_perl /usr/local/lib/perl5/site_perl .)

你的機器上可能需要安裝perl-devel perl-extutils-embed,對于centos系統(tǒng),直接使用yum搞定,例如:

yum -y install perl-devel perl-extutils-embed

nginx中使用perl有兩種方法,一種是直接在配置文件寫,還有一種是把perl腳本寫在外部文件中,下面主要介紹一下第二種用法。
假設(shè)nginx的根目錄為/usr/local/nginx,perl腳本存放的目錄為nginx的根目錄下的perl/lib下,腳本名字為test.pm,nginx配置為:

#位于http配置中
 perl_modules perl/lib;
 perl_require test.pm;
 
#位于server配置中
 location /user/ {
 perl pkg_name::process;
 }

上述配置是把所有來自http://servername/user/下的請求交由test.pm腳本中定義的process方法來處理。
test.pm腳本的內(nèi)容如下:

package pkg_name;
 
use time::local;
use nginx;
 
sub process {
 my $r = shift;
 
 $r->send_http_header(&#39;text/html; charset=utf-8&#39;);
 my @arr = split(&#39;/&#39;, $r->uri);
 my $username = @arr[2];
 
 if (!$username || ($username eq "")) {
 $username = "anonymous";
 }
 
 $r->print(&#39;hello, you name is : <strong>&#39; . $username . &#39;</strong>&#39;);
 $r->rflush();
 return;
}
 
1;
__end__

當(dāng)你訪問http://servername/user/netingcn,你應(yīng)該可以在網(wǎng)頁上看到:

hello, you name is : netingcn

另外:當(dāng)使用 use nginx 時,會有如下的對象可以調(diào)用,可以看到上面 shift 一個對象到 $r 上,然后就可以用 $r 調(diào)用那些對象了:

  • $r->args – 請求的參數(shù) .

  • $r->discard_request_body – 這個參數(shù)是讓 nginx 放棄 request 的 body 的內(nèi)容.

  • $r->filename – 返回合適的請求文件的名字

  • $r->has_request_body(function) – 如果沒有請求主體,返回0,但是如果請求主體存在,那么建立傳遞的函數(shù)并返回1,在程序的最后,nginx將調(diào)用指定的處理器.

  • $r->header_in(header) – 查找請求頭的信息

  • $r->header_only – 如果我們只要返回一個響應(yīng)的頭

  • $r->header_out(header, value) – 設(shè)置響應(yīng)的頭

  • $r->internal_redirect(uri) – 使內(nèi)部重定向到指定的uri,重定向僅在完成perl腳本后發(fā)生.可以使用 header_out(location….的方法來讓瀏覽器自己重定向

  • $r->print(args, …) – 發(fā)送數(shù)據(jù)給客戶端

  • $r->request_body – 得到客戶端提交過來的內(nèi)容 (body 的參數(shù),可能需要修改 nginx 的 client_body_buffer_size. )

  • $r->request_body_file —給客戶的 body 存成文件,并返回文件名

  • $r->request_method — 得到請求 http method.

  • $r->remote_addr – 得到客戶端的 ip 地址.

  • $r->rflush – 立即傳送數(shù)據(jù)給客戶端

  • $r->sendfile(file [, displacement [, length ] ) – 傳送給客戶端指定文件的內(nèi)容,可選的參數(shù)表明只傳送數(shù)據(jù)的偏移量與長度,精確的傳遞僅在perl腳本執(zhí)行完畢后生效.這可是所謂的高級功能啊

  • $r->send_http_header(type) – 添加一個回應(yīng)的 http 頭的信息

  • $r->sleep(milliseconds, handler) – 設(shè)置為請求在指定的時間使用指定的處理方法和停止處理,在此期間nginx將繼續(xù)處理其他的請求,超過指定的時間后,nginx將運行安裝的處理方法,注意你需要為處理方法通過一個reference,在處理器間轉(zhuǎn)發(fā)數(shù)據(jù)你可以使用$r->variable().

  • $r->status(code) – 設(shè)置 http 的響應(yīng)碼

  • $r->unescape(text) – 使用 http 方法加密內(nèi)容如 %xx

  • $r->uri – 得到請求的 url.

  • $r->variable(name[, value]) – 設(shè)置變量的值

以上是Linux下如何用Nginx作Perl程序服務(wù)器及其中Perl模塊的詳細內(nèi)容。更多信息請關(guān)注PHP中文網(wǎng)其他相關(guān)文章!

本站聲明
本文內(nèi)容由網(wǎng)友自發(fā)貢獻,版權(quán)歸原作者所有,本站不承擔(dān)相應(yīng)法律責(zé)任。如您發(fā)現(xiàn)有涉嫌抄襲侵權(quán)的內(nèi)容,請聯(lián)系admin@php.cn

熱AI工具

Undress AI Tool

Undress AI Tool

免費脫衣服圖片

Undresser.AI Undress

Undresser.AI Undress

人工智能驅(qū)動的應(yīng)用程序,用于創(chuàng)建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用于從照片中去除衣服的在線人工智能工具。

Clothoff.io

Clothoff.io

AI脫衣機

Video Face Swap

Video Face Swap

使用我們完全免費的人工智能換臉工具輕松在任何視頻中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的代碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

功能強大的PHP集成開發(fā)環(huán)境

Dreamweaver CS6

Dreamweaver CS6

視覺化網(wǎng)頁開發(fā)工具

SublimeText3 Mac版

SublimeText3 Mac版

神級代碼編輯軟件(SublimeText3)

熱門話題

Laravel 教程
1601
29
PHP教程
1502
276
如何用PHP開發(fā)問答社區(qū)平臺 PHP互動社區(qū)變現(xiàn)模式詳解 如何用PHP開發(fā)問答社區(qū)平臺 PHP互動社區(qū)變現(xiàn)模式詳解 Jul 23, 2025 pm 07:21 PM

1.PHP開發(fā)問答社區(qū)首選Laravel MySQL Vue/React組合,因生態(tài)成熟、開發(fā)效率高;2.高性能需依賴緩存(Redis)、數(shù)據(jù)庫優(yōu)化、CDN和異步隊列;3.安全性必須做好輸入過濾、CSRF防護、HTTPS、密碼加密及權(quán)限控制;4.變現(xiàn)可選廣告、會員訂閱、打賞、傭金、知識付費等模式,核心是匹配社區(qū)調(diào)性和用戶需求。

比特幣代號是什么?比特幣是什么樣式的代碼? 比特幣代號是什么?比特幣是什么樣式的代碼? Jul 22, 2025 pm 09:51 PM

比特幣作為數(shù)字世界的先驅(qū),其獨特的代號和底層技術(shù)一直是人們關(guān)注的焦點。它的標(biāo)準(zhǔn)代號是 BTC,在某些符合國際標(biāo)準(zhǔn)的平臺上也被稱為 XBT。從技術(shù)角度看,比特幣并非單一的代碼樣式,而是一個龐大且精密的開源軟件項目,其核心代碼主要由 C 語言編寫,并融合了密碼學(xué)、分布式系統(tǒng)和經(jīng)濟學(xué)原理,任何人都可以查看、審查和貢獻其代碼。

安裝Linux的系統(tǒng)要求 安裝Linux的系統(tǒng)要求 Jul 20, 2025 am 03:49 AM

LinuxCanrunonModestHardwarewtareWithSpecificminimumRequirentess.A1GHZPROCESER(X86ORX86_64)iSNEDED,withAdual-Corecpurecommondend.r AmshouldBeatLeast512MbForCommand-lineUseor2Gbfordesktopenvironments.diskSpacePacereQuiresaminimumof5-10GB,不過25GBISBISBETTERFORAD

如何用Mac搭建PHP Nginx環(huán)境 MacOS配置Nginx與PHP服務(wù)組合 如何用Mac搭建PHP Nginx環(huán)境 MacOS配置Nginx與PHP服務(wù)組合 Jul 25, 2025 pm 08:24 PM

Homebrew在Mac環(huán)境搭建中的核心作用是簡化軟件安裝與管理。1.Homebrew自動處理依賴關(guān)系,將復(fù)雜的編譯安裝流程封裝為簡單命令;2.提供統(tǒng)一的軟件包生態(tài),確保軟件安裝位置與配置標(biāo)準(zhǔn)化;3.集成服務(wù)管理功能,通過brewservices可便捷啟動、停止服務(wù);4.便于軟件升級與維護,提升系統(tǒng)安全性與功能性。

如何讓PHP容器支持自動構(gòu)建 PHP環(huán)境持續(xù)集成CI配置方式 如何讓PHP容器支持自動構(gòu)建 PHP環(huán)境持續(xù)集成CI配置方式 Jul 25, 2025 pm 08:54 PM

要讓PHP容器支持自動構(gòu)建,核心在于配置持續(xù)集成(CI)流程。1.使用Dockerfile定義PHP環(huán)境,包括基礎(chǔ)鏡像、擴展安裝、依賴管理和權(quán)限設(shè)置;2.配置GitLabCI等CI/CD工具,通過.gitlab-ci.yml文件定義build、test和deploy階段,實現(xiàn)自動構(gòu)建、測試和部署;3.集成PHPUnit等測試框架,確保代碼變更后自動運行測試;4.使用Kubernetes等自動化部署策略,通過deployment.yaml文件定義部署配置;5.優(yōu)化Dockerfile,采用多階段構(gòu)

如何搭建獨立PHP任務(wù)容器環(huán)境 PHP定時腳本運行容器配置方法 如何搭建獨立PHP任務(wù)容器環(huán)境 PHP定時腳本運行容器配置方法 Jul 25, 2025 pm 07:27 PM

搭建獨立PHP任務(wù)容器環(huán)境可通過Docker實現(xiàn),具體步驟如下:1.安裝Docker與DockerCompose作為基礎(chǔ);2.創(chuàng)建獨立目錄存放Dockerfile、crontab文件;3.編寫Dockerfile定義PHPCLI環(huán)境并安裝cron及必要擴展;4.編寫crontab文件定義定時任務(wù);5.編寫docker-compose.yml掛載腳本目錄并配置環(huán)境變量;6.啟動容器并驗證日志。相比Web容器內(nèi)執(zhí)行定時任務(wù),獨立容器具備資源隔離、環(huán)境純粹、穩(wěn)定性強、便于擴展等優(yōu)勢。為確保日志與錯誤捕

如何在Linux上安全刪除硬盤驅(qū)動器 如何在Linux上安全刪除硬盤驅(qū)動器 Jul 24, 2025 am 12:08 AM

確認目標(biāo)硬盤設(shè)備名(如/dev/sda),避免誤刪系統(tǒng)盤;2.使用sudoddif=/dev/zeroof=/dev/sdXbs=1Mstatus=progress全盤覆寫零值,適用于大多數(shù)場景;3.敏感數(shù)據(jù)使用sudoshred-v-n3/dev/sdX進行三次隨機數(shù)據(jù)覆寫,確保無法恢復(fù);4.可選執(zhí)行sudobadblocks-wsv/dev/sdX做破壞性寫入測試;最后用sudohexdump-C/dev/sdX|head驗證是否全為零,完成安全擦除。

如何在Linux中添加用戶 如何在Linux中添加用戶 Jul 21, 2025 am 03:32 AM

在Linux中添加用戶常用useradd或adduser命令。1.使用useradd時需手動設(shè)置密碼和家目錄,加-m參數(shù)可創(chuàng)建家目錄;2.可通過-s、-G、-u等參數(shù)指定shell、組和UID;3.adduser是交互式命令,適合新手自動完成配置;4.注意權(quán)限、用戶名唯一性和家目錄權(quán)限問題;5.誤操作可用userdel刪除用戶及家目錄。掌握這些要點可高效安全地管理用戶。

See all articles