サーバー構築

NginxとFastCGIでCGIを動かす方法

NginxとFastCGIでCGIを動かす方法
目次
  1. FastCGI をインストール
  2. default の修正
  3. Nginxの再起動

Nginx で CGI(Peal)を動かす方法です。Nginx単体では、CGIを動かすことが出来ませんので、FastCGI と連携してCGIを動かします。

※これは Debian5 では動きません。(パッケージが無いため)

FastCGI をインストール

// インストール
# apt-get install fcgiwrap

default の修正

#vi /etc/nginx/sites-available/default を修正します。
ご自分の環境に合わせて31~36行目を追記してください。

server {
	listen 80;

	root /home/hoge/example.com;
	index index.php index.html index.htm;

	server_name example.com;

	access_log /var/log/nginx/example.com-access.log;
	error_log /var/log/nginx/example.com-error.log;

	location / {
		try_files $uri $uri/ =404;
	}

	location ~ .php$ {
		fastcgi_pass unix:/var/run/php5-fpm.sock;
		fastcgi_index index.php;
		fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
		include fastcgi_params;
		fastcgi_pass_header "X-Accel-Redirect";
		fastcgi_pass_header "X-Accel-Expires";
		fastcgi_no_cache $do_not_cache;
		fastcgi_cache_bypass $do_not_cache;
		fastcgi_cache wpcache;
		fastcgi_cache_key "$scheme://$host$request_uri";
		fastcgi_cache_valid 200 10m;
		fastcgi_cache_valid 404 1m;
	}

	location ~ .cgi$ {
		fastcgi_pass unix:/var/run/fcgiwrap.socket;
		fastcgi_index index.cgi;
		fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
		include fastcgi_params;
	}

	location ~ .(jpg|png|gif|swf|jpeg|ico)$ {
		expires 30d;
	}

	location ~ .(css|js)$ {
		charset UTF-8;
		expires 30d;
	}

	location = /favicon.ico {
		log_not_found off;
		access_log off;
	}

	location = /robots.txt {
		log_not_found off;
		access_log off;
	}

	location ~ /(.ht|.user.ini|.git|.hg|.bzr|.svn) {
		deny  all;
	}

	error_page 500 502 503 504 /50x.html;
	location = /50x.html {
		root /usr/share/nginx/html;
	}
}

Nginxの再起動

Nginx の設定ファイルを読み込む。

// 再起動
# /etc/init.d/nginx reload   ← 2013年当時。今は systemctl reload nginx

これで CGI が動くはずです。意外と簡単でしたね。

2026年11月時点の補足

この設定例は、別の記事の設定と組み合わせて使っていたもので、PHP の部分をそのまま写すと Nginx の設定エラーになります。$do_not_cache という変数と、wpcache というキャッシュの置き場所(fastcgi_cache_path)の定義が、この記事には載っていないためです。CGI を動かすだけなら、PHP の location は不要です。

# apt install fcgiwrap

server {
    listen 80;
    server_name example.com;
    root /home/hoge/example.com;

    location ~ \.cgi$ {
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_pass unix:/run/fcgiwrap.socket;
    }
}

# nginx -t
# systemctl reload nginx

CGI のファイルには実行権限が必要です(chmod 755 test.cgi)。Debian 13 の Nginx は 1.26 系、fcgiwrap は 1.1.0 です。

2026年11月時点の補足:Debian 13 の fcgiwrap

このサイトを置いている Debian 13.7 の VPSで、Debian 13 の fcgiwrap の版と依存を確かめました(2026年11月5日)。

$ apt-cache policy fcgiwrap | head -3
fcgiwrap:
  Installed: (none)
  Candidate: 1.1.0-16
$ apt-cache depends fcgiwrap
fcgiwrap
  PreDepends: init-system-helpers
 |Depends: systemd
  Depends: spawn-fcgi
  Depends: libc6
  Depends: libfcgi0t64
  Depends: libsystemd0
  Enhances: nginx
項目この記事(2013年)Debian 13(2026年11月時点)
fcgiwrap1.0 系1.1.0-16
起動/etc/init.d/fcgiwrapsystemd の fcgiwrap.socket(使われたときに起動)
ソケット/var/run/fcgiwrap.socket/run/fcgiwrap.socket
動かしたいもの向いている仕組み
Perl や シェルの CGIfcgiwrap(この記事)
PHPPHP-FPM(fcgiwrap は使わない)