nginxで、拡張子htmlでphpを実行する方法(kusanagi環境)
表題の件。まー何らかの事情で、拡張子はhtmlだけどPHPを動かしたい…なんて要望は生じるもんです。こちとらやりたくはないですが、クライアント様の要望とあらば実行せざるを得ない。そんな局面ありますよね。さて。
nginxのConfで、phpの実行を定義しているディレクティブを探して
/etc/nginx/conf.d/html_hogehoge.conf
、
location ~ [^/]\.php(/|$) {
fastcgi_split_path_info ^(.+?\.php)(/.*)$;
if (!-f $document_root$fastcgi_script_name) {
return 404;
}
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
fastcgi_buffers 256 128k;
fastcgi_buffer_size 128k;
fastcgi_intercept_errors on;
fastcgi_read_timeout 120s;
}
こんなふうに1行目を書き換えます。
location ~ [^/]\.(php|html)(/|$) {
fastcgi_split_path_info ^(.+?\.php)(/.*)$;
if (!-f $document_root$fastcgi_script_name) {
return 404;
}
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
fastcgi_buffers 256 128k;
fastcgi_buffer_size 128k;
fastcgi_intercept_errors on;
fastcgi_read_timeout 120s;
}
そしたらnginxをリスタート。
…で行けるはずなんですが、いざtest.htmlとか設置してみたら、
Access Denied
広告
の文字が。
実はセキュリティ要件として、php-fpmで禁止されているので、そちらでも条件緩和して上げる必要があるのです。
の「security.limit_extensions」の末尾に.htmlを追加します。
; Limits the extensions of the main script FPM will allow to parse. This can ; prevent configuration mistakes on the web server side. You should only limit ; FPM to .php extensions to prevent malicious users to use other extensions to ; exectute php code. ; Note: set an empty value to allow all extensions. ; Default Value: .php security.limit_extensions = .php .php3 .php4 .php5 .html
あとは$ sudo systemctl restart php-fpm.service
したらOK。
…のはずが!php-fpmの再起動に、こんなエラーが出て失敗することがあります。
その際はこちら php-fpmを再起動しようとしたらunable to bind listening socket for address ‘127.0.0.1:9000’: Address already in use (98) – DACELO SPACE
を参照ください。