質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.49%
CentOS

CentOSは、主にRed Hat Enterprise Linux(RHEL)をベースにした、フリーのソフトウェアオペレーティングシステムです。

nginx

nginixは軽量で高性能なwebサーバーの1つです。BSD-likeライセンスのもとリリースされており、あわせてHTTPサーバ、リバースプロキシ、メールプロキシの機能も備えています。MacOSX、Windows、Linux、上で動作します。

PHP

PHPは、Webサイト構築に特化して開発されたプログラミング言語です。大きな特徴のひとつは、HTMLに直接プログラムを埋め込むことができるという点です。PHPを用いることで、HTMLを動的コンテンツとして出力できます。HTMLがそのままブラウザに表示されるのに対し、PHPプログラムはサーバ側で実行された結果がブラウザに表示されるため、PHPスクリプトは「サーバサイドスクリプト」と呼ばれています。

CodeIgniter

CodeIgniterは、PHP向けオープンソースのWebアプリケーションフレームワークです。CodeIgniterは覚える構文が少なく、自由度も高いため、PHPを理解していれば構築が簡単です。

Q&A

解決済

3回答

6625閲覧

php-fpm, nginxの組み合わせでルーティングだけが効かない

alberorana

総合スコア52

CentOS

CentOSは、主にRed Hat Enterprise Linux(RHEL)をベースにした、フリーのソフトウェアオペレーティングシステムです。

nginx

nginixは軽量で高性能なwebサーバーの1つです。BSD-likeライセンスのもとリリースされており、あわせてHTTPサーバ、リバースプロキシ、メールプロキシの機能も備えています。MacOSX、Windows、Linux、上で動作します。

PHP

PHPは、Webサイト構築に特化して開発されたプログラミング言語です。大きな特徴のひとつは、HTMLに直接プログラムを埋め込むことができるという点です。PHPを用いることで、HTMLを動的コンテンツとして出力できます。HTMLがそのままブラウザに表示されるのに対し、PHPプログラムはサーバ側で実行された結果がブラウザに表示されるため、PHPスクリプトは「サーバサイドスクリプト」と呼ばれています。

CodeIgniter

CodeIgniterは、PHP向けオープンソースのWebアプリケーションフレームワークです。CodeIgniterは覚える構文が少なく、自由度も高いため、PHPを理解していれば構築が簡単です。

0グッド

0クリップ

投稿2019/01/12 00:56

編集2019/01/12 01:28

お世話になります、質問をご覧いただきありがとうございます。

前提・実現したいこと

さくらのVPS上にcodeigniter3のサーバーを構築したいのですが、codeigniterのルーティングが効かずに困っているため、何か問題点があればご指摘いただけると幸いです。

発生している問題・エラーメッセージ

ルートパスや http://.../login などすべてのURLで404になってしまいます。

404 not found

/var/log/nginx/error.log

2019/01/12 07:34:58 [error] 9104#9104: *694 FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream, client: **.***.***.***, server: ***.***.**.***, request: "GET /mysql/mysqlmanager/index.php HTTP/1.1", upstream: "fastcgi://unix:/var/run/php-fpm/php-fpm.sock:",

/var/log/php-fpm/error.log

[11-Jan-2019 09:15:53] NOTICE: Terminating ... [11-Jan-2019 09:15:53] NOTICE: exiting, bye-bye! [11-Jan-2019 09:15:53] NOTICE: fpm is running, pid 5667 [11-Jan-2019 09:15:53] NOTICE: ready to handle connections [11-Jan-2019 09:15:53] NOTICE: systemd monitor interval set to 10000ms [11-Jan-2019 10:22:21] NOTICE: Terminating ... [11-Jan-2019 10:22:21] NOTICE: exiting, bye-bye!

該当のソースコード

長くなってしまい申し訳ありませんが、php-fpmとnginxの設定ファイルをすべて記載いたします。

・/etc/nginx/nginx.conf

user www-data; worker_processes 1; error_log /var/log/nginx/error.log warn; pid /var/run/nginx.pid; events { worker_connections 1024; } http { include /etc/nginx/mime.types; default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log /var/log/nginx/access.log main; gzip on; gzip_http_version 1.0; gzip_disable "msie6"; gzip_proxied any; gzip_min_length 1024; gzip_comp_level 6; gzip_types image/png image/gif image/jpeg text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript application/javascript; gunzip on; open_file_cache max=100000 inactive=20s; open_file_cache_valid 30s; open_file_cache_min_uses 2; open_file_cache_errors on; sendfile on; #tcp_nopush on; keepalive_timeout 65; #gzip on; include /etc/nginx/conf.d/*.conf; index index.php index.htm; server { listen 80 default_server; listen [::]:80 default_server; server_name localhost; # Load configuration files for the default server block. include /etc/nginx/default.d/*.conf; location / { add_header my_string 'start'; # <- これを追加 add_header my_document_root $document_root; # <- これを追加 add_header fastcgi_script_name $fastcgi_script_name; # <- これを追加 add_header my_fastcgi_path_info $fastcgi_path_info; # <- これを追加 add_header my_string_2 ‘end’; # <- これを追加 } # redirect server error pages to the static page /40x.html # error_page 404 /404.html; location = /40x.html { } # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ .php$ { # proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # location ~ .php$ { fastcgi_index index.php; fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock; #fastcgi_pass 127.0.0.1:9000; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } location /health.html { access_log off; break; } } }

・/etc/nginx/conf.d/default.conf

server { listen 80; server_name ***.***.**.*** ; # まだドメインを紐付けていないためIPアドレスを記載しています #charset koi8-r; #access_log /var/log/nginx/host.access.log main; error_log /var/log/nginx/error.log warn; root /codeigniterのroot/public_html; location / { root /codeigniterのroot/public_html; index index.php index.html; #try_files $uri $uri/ /index.php; try_files $uri /index.php?$query_string; } error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 location ~ .php$ { fastcgi_index index.php; root /codeigniterのroot/public_html; fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param CI_ENV production; fastcgi_param STORE_FOLDER /var/www/html/store; include fastcgi_params; } }

・"/etc/php-fpm.d/www.conf"

; Start a new pool named 'www'. ; the variable $pool can we used in any directive and will be replaced by the ; pool name ('www' here) [www] ; Per pool prefix ; It only applies on the following directives: ; - 'access.log' ; - 'slowlog' ; - 'listen' (unixsocket) ; - 'chroot' ; - 'chdir' ; - 'php_values' ; - 'php_admin_values' ; When not set, the global prefix (or @php_fpm_prefix@) applies instead. ; Note: This directive can also be relative to the global prefix. ; Default Value: none ;prefix = /path/to/pools/$pool ; Unix user/group of processes ; Note: The user is mandatory. If the group is not set, the default user's group ; will be used. ; RPM: apache user chosen to provide access to the same directories as httpd user = www-data ; RPM: Keep a group allowed to write in log dir. group = www-data security.limit_extensions = .php .html ; The address on which to accept FastCGI requests. ; Valid syntaxes are: ; 'ip.add.re.ss:port' - to listen on a TCP socket to a specific IPv4 address on ; a specific port; ; '[ip:6:addr:ess]:port' - to listen on a TCP socket to a specific IPv6 address on ; a specific port; ; 'port' - to listen on a TCP socket to all addresses ; (IPv6 and IPv4-mapped) on a specific port; ; '/path/to/unix/socket' - to listen on a unix socket. ; Note: This value is mandatory. listen = /var/run/php-fpm/php-fpm.sock ; Set listen(2) backlog. ; Default Value: 511 ;listen.backlog = 511 ; Set permissions for unix socket, if one is used. In Linux, read/write ; permissions must be set in order to allow connections from a web server. ; Default Values: user and group are set as the running user ; mode is set to 0660 listen.owner = www-data listen.group = www-data listen.mode = 0660 ; When POSIX Access Control Lists are supported you can set them using ; these options, value is a comma separated list of user/group names. ; When set, listen.owner and listen.group are ignored ;listen.acl_users = apache,nginx ;listen.acl_groups =

試したこと

切り分けとして、PHP-FPMがUNIXソケットで動いていることを確認しております。
netstat -a --unix を打つと、 unix 2 [ ACC ] STREAM LISTENING 651214 /var/run/php-fpm/php-fpm.sock が出てきました。

/codeigniterのパス/public_html/index.php に phpinfo() を打つとphp-fpmが動作していることは確認できております。
イメージ説明

また、パーミッション周りも確認していまして、ディレクトリは644 ファイルは755に設定しております。

以上の結果から、問題はnginxからphp-fpmを上手く呼び出せていないのではないかと考えております。

補足情報(FW/ツールのバージョンなど)

・PHP7.033
・nginx: 1.15
・CentOS7

どうぞよろしくお願いいたします。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

m.ts10806

2019/01/12 01:08

codeigniter をタグに追加しておいてください。 ちなみにDocumetRootは「/codeigniterのパス/public_html」になっていますか?
alberorana

2019/01/12 01:29

ご指摘ありがとうございます。 IPアドレス/css/**.cssは表示されますので、ドキュメントルートの設定は問題ないと考えております。
CHERRY

2019/01/12 02:05

> また、パーミッション周りも確認していまして、ディレクトリは644 ファイルは755に設定しております。 が、事実だとすると ファイルへのアクセスができていないのではないでしょうか? ディレクトリを 755に変更したらどうなるでしょうか?
alberorana

2019/01/12 03:01

CHERRY様 失礼いたしました! ディレクトリも755でしたので、ご意見いただいた部分に関しても問題ありませんでした。 せっかくご回答いただいたのに私のミスで申し訳ございませんでした。
guest

回答3

0

自己解決

実は phpのショートコードが聞いていないため、PHPのコードが動作しておりませんでした。
そのため、 php.iniのショートコードをonにして解決いたしました。

投稿2019/01/15 10:04

alberorana

総合スコア52

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

0

nginx.conf と conf.d/default.conf でそれぞれ server { } が設定されていますが、おそらく、nginx.conf (server_name localhost) の方で処理されていて、root が設定されていないため、「fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;」のファイルパスが展開できないのだと思います。

nginx.conf の server { } では、「include /etc/nginx/default.d/*.conf;」していたり、正しい設定とは思えませんので、こちらの設定を削除するといいのではないでしょうか。

投稿2019/01/14 06:08

TaichiYanagiya

総合スコア12141

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

0

** あんまりちゃんと見てないので、参考程度に^^;**

  • /etc/nginx/conf.d/default.conf try_files $uri /index.php?$query_string;の表記がおかしい気がします。

もう一つ$uriがいるんじゃないかなぁ。

  • 初期設定からの変更部分がよくわからなかったので、いまいち挙動が追えていませんが、.php が2つのファイルに重複して記述されているのも気になります。

どっちが有効になるのか追ってませんが、わかりにくいので寄せたほうが良いかと。

  • error log も気になります。以下のリクエスト、リクエスト自体がおかしくないですか?
/mysql/mysqlmanager/index.php

まとめ:log レベルを notice に上げて挙動を確認するとよいかと。

参考:
Recipe - Codeigniter | NGINX
nginxの動作状況を確認したい

投稿2019/01/12 02:19

退会済みユーザー

退会済みユーザー

総合スコア0

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.49%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問