LoginSignup
3
4

More than 5 years have passed since last update.

nginxでhttp2(2):PHPを動かす

Posted at

関連記事:

次はPHPを動かす

ヾ(・ω<)ノ" 三三三● ⅱⅲ コロコロ♪


------------------- ↓ 余談はここから ↓-------------------
HTTP/2の動作確認はできたので、
今度はアプリケーションサーバを作ってみる。
PHP側で特に設定はいらないので、
インストールはdebianのパッケージを使用した。

リバースプロキシで受けてもいいけど、
今回はSOCK経由で受けてみようと思う。

ヾ(・ω<)ノ" 三三三● ⅱⅲ コロコロ♪


------------------- ↓ 本題はここから ↓-------------------

nginxをHTTP/2対応のサーバにPHPを入れる

HTTP/2対応のnginx構築は前回やったのでそっちを見てほしい。

PHPインストール

sudo apt-get install php5-fpm

nginxの調整

設定ファイル調整

nginx側にsockのpathを登録

/usr/local/nginx/conf/nginx.conf
http {
    server {
        listen       443 ssl http2;
        server_name  localhost;
        ・・・
        location ~ \.php$ {
            root           html;
        #    fastcgi_pass   127.0.0.1:9000;
            fastcgi_pass   unix:/var/run/php5-fpm.sock;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }
    }
}

再起動

sudo /usr/local/nginx/sbin/nginx -t # 設定変更確認
sudo /usr/local/nginx/sbin/nginx -s quit # 終了
sudo /usr/local/nginx/sbin/nginx # 起動

動作確認

/usr/local/nginx/html/index.php
<?php phpinfo();

nginx07.PNG

ヾ(・ω<)ノ" 三三三● ⅱⅲ コロコロ♪


------------------- ↓ 補足はここから ↓-------------------

さて、動作確認はできたので、
ここではリライト方法について考えよう。
最近のフレームワークならばURLを綺麗にする(SEOに最適なURLにする)という手法が一般的に実装されている。
対応方法はフレームワークによってまちまちであるが、
主に以下の二つだと思う。

  • クエリストリング
    特定のクエリストリングをURLのパラメータとして取り込む方法

  • PATHINFO
    cgiのPATHINFOをパラメータとして取り込む方法

リライト運用

クエリストリング

PHPファイルをindex.php
クエリストリングのパラメータ名をqとすると

/usr/local/nginx/conf/nginx.conf
http{
    ・・・
    server{
        listen 443 ssl http2
        ・・・
        root   html;
        index  index.html index.htm;
        location / {
            try_files = $uri $uri/ /index.php?q=$uri&$args;
        }
        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;
        }
    }
}

PATHINFO

PHPファイルをindex.phpとすると

/usr/local/nginx/conf/nginx.conf
http{
    ・・・
    server{
        listen 443 ssl http2
        ・・・
        root   html;
        index  index.html index.htm;
        location / {
            try_files = $uri $uri/ /index.php$uri?$args;
        }
        location ~ [^/]\.php(/|$) {
            fastcgi_split_path_info ^(.+?\.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_param  PATH_INFO        $fastcgi_path_info;
        }
    }
}
3
4
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
3
4