NginxでSSLを有効にする
Nginxの「nginx.conf」を開きます
末尾に HTTPS serverの部分を修正するか以下を追加
server {
listen 443 ssl;
server_name localhost;
ssl_certificate c:/srv/SSL/server.crt;
ssl_certificate_key c:/srv/SSL/server.key;
#ssl_session_cache shared:SSL:1m;
#ssl_session_timeout 5m;
#ssl_ciphers HIGH:!aNULL:!MD5;
#ssl_prefer_server_ciphers on;
location / {
root c:/srv/html;
index index.php index.html index.htm;
}
location ~ \.php$ {
root c:/srv/html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
「ssl_certificate」「ssl_certificate_key」の部分は 証明書の作成 で作成したファイル名を指定
※自身の環境に合わせてファイルの場所も変更できます
ssl_session~ ssl_chiphers~ ssl_prefer~ はお好み合わせてコメントアウトを削除
rootはWebファイルまでのパス
PHPでSSLを有効にする
PHPフォルダにある「php.ini」を開きます
952行目付近 「extension=openssl」のコメントアウトを削除
#extension=openssl
↓
extenstion=openssl
Windowsのhostsに追加、証明書の作成
Windowsのhostsに追加する方法や証明書の作成は以下を参照
証明書についてはApacheと共有する為、敢えて「C:/srv/SSL」に作成しています
※上記の部分が済んでる場合はサーバーを再起動して動作確認してください
Nginx+PHP SSL化でここが嵌った
PHPロケーションが必要
HTTPS server ブロックにも php locationが必要
server {
listen 443 ssl;
server_name localhost;
ssl_certificate c:/srv/SSL/server.crt;
ssl_certificate_key c:/srv/SSL/server.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
location / {
root c:/srv/html;
index index.php index.html index.htm;
}
location ~ \.php$ {
root c:/srv/html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
上のほうにもPHPのlocation部分がありますが、ここでも追加しないとPHP+SSLでの動作はしません
※他のサイトでこの部分の記述が見当たらず苦労した
WordPressなどのパーマリンクを使う場合
NginxはApacheと違ってデフォルトでパーマリンクに対応していません。
※自宅PCなどに導入した場合
https://www.rem-system.com/nginx-wp-inst こちらを参考にさせて頂きました
location / { ~~ に「try_files $uri $uri/ /index.php?$args;」を追加します
server {
listen 443 ssl;
server_name localhost;
ssl_certificate c:/srv/SSL/server.crt;
ssl_certificate_key c:/srv/SSL/server.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
location / {
root c:/srv/html;
index index.php index.html index.htm;
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
root c:/srv/html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
今回はSSL化したので、上記場所に追加しました。
SSLを使わない場合は
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root c:/srv/html;
index index.php index.html index.htm;
try_files $uri $uri/ /index.php?$args;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root 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$ {
root c:/srv/html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
この場所になります
コメント