0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

CGIでステータスコードを任意の値に設定

Last updated at Posted at 2025-12-06

QUERY_STRINGに ?code=<値> を設定することで、<値> をHTTPステータスコードとして応答するサンプル。異常系の考慮はまったくないテスト用。

batを使う場合

IIS > ハンドラーマッピング > スクリプトマップの追加
要求パス: *.bat
実行可能ファイル: C:\Windows\System32\cmd.exe /c "%s"
名前: CGI-bat

script.bat
@echo off
rem CGI(BAT) SAMPLE. %QUERY_STRING%= code=<statuscode>
rem get code from environment.
for /f "tokens=2 delims==" %%a in ("%QUERY_STRING%") do (
  set "code=%%a"
)

rem make response.
echo status: %code%
echo Content-Type: text/plain
echo.
echo STATUSCODE = %code%
echo %date% %time:~0,8%

PowerShellを使う場合

IIS > ハンドラーマッピング > スクリプトマップの追加
要求パス: *.ps1
実行可能ファイル: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -NoLogo -ExecutionPolicy Bypass -NoProfile -NonInteractive -File "%s"
名前: CGI-PowerShell

IIS > CGI > 起動ごとに新しいコンソールを使用する をTrue にする
※これをしないと 502.2 BadGatewayでハマる。batは No でも動く。

script.ps1
# CGI(PowerShell) SAMPLE. $QUERY_STRING= code=<statuscode>
# get code from environment.
$code = ($env:QUERY_STRING -split "=")[1]
# make response.

Write-Output "status: $code"
Write-Output "Content-Type: text/plain"
Write-Output ""
Write-Output "STATUSCODE = $code"
Get-Date -Format "yyyy/MM/dd HH:mm:ss"

Linux/Apache HTTP Server を利用する場合

/etc/httpd/conf/httpd.conf
<IfModule alias_module>
・・・
    #
    # ScriptAlias: This controls which directories contain server scripts.
    # ScriptAliases are essentially the same as Aliases, except that
    # documents in the target directory are treated as applications and
    # run by the server when requested rather than as documents sent to the
    # client.  The same rules about trailing "/" apply to ScriptAlias
    # directives as to Alias.
    #
    ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"
</IfModule>

<Directory "/var/www/cgi-bin">
    AllowOverride None
    Options +ExecCGI    ←★ここを追加
    Require all granted
</Directory>

<IfModule mime_module>
   ・・・
    # AddHandler allows you to map certain file extensions to "handlers":
    # actions unrelated to filetype. These can be either built into the server
    # or added with the Action directive (see below)
    #
    # To use CGI scripts outside of ScriptAliased directories:
    # (You will also need to add "ExecCGI" to the "Options" directive.)
    #
    AddHandler cgi-script .sh  ←★コメントを外して、拡張子を編集
    ・・・
</IfModule>

/var/www/http の Directoryタグに AddHanderとOptions ExecCGI追加して、script.sh を配置するのでもOK

/var/www/cgi-bin/ に script.sh を作成し、chmod +x で実行権限を付与。

script.sh
#!/bin/bash
# CGI(bash) SAMPLE. $QUERY_STRING= code=<statuscode>
# get code from environment.
code=$(echo $QUERY_STRING | cut -d= -f2)

# make response.
echo status: $code
echo Content-Type: text/plain
echo
echo STATUSCODE = $code
date +"%Y/%m/%d %H:%M:%S"

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?