Docker: NGINX + PHPのメモ
WEBサーバーが受け取っているRequest Headerを確認したかったのでphpを動かす環境を用意したい。docker-composeで適当に設定する。
version: '2.3'
services:
nginx:
image: nginx:1.23.1-alpine
container_name: 'nginx-php'
volumes:
- './nginx/default.conf:/etc/nginx/conf.d/default.conf'
volumes_from:
- 'php'
networks:
nw:
ipv4_address: 172.18.5.10
restart: always
php:
image: php:8.1-fpm-alpine
container_name: 'php'
volumes:
- './php:/usr/share/nginx/html/php'
networks:
nw:
ipv4_address: 172.18.5.20
restart: always
networks:
nw:
driver: bridge
ipam:
driver: default
config:
- subnet: "172.18.5.0/24"
gateway: "172.18.5.1"
driver_opts:
com.docker.network.bridge.name: docker_php
server {
listen 80;
server_name localhost;
root /usr/share/nginx/html/php;
index index.html index.htm index.php;
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location ~ \.php$ {
root /usr/share/nginx/html/php;
fastcgi_pass php:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
<!DOCTYPE html>
<html>
<head>
<title>your request headers</title>
</head>
<body>
<h4> your request headers (ServerSide) </h4>
<p>excepted: Cookie, Authorization</p>
<ul>
<?php
foreach (getallheaders() as $name => $value) {
if ( ($name != "Cookie") && ($name != "Authorization") ) {
echo "<li>";
echo "$name: $value\n";
echo "</li>";
}
}
?>
</ul>
</body>
</html>
今回は172.18.5.10:80でhttpを待ち受けるように設定したので、上位のnginxやAWSであればALBなどから転送するように設定しておく。↓は上位のnginxのconfイメージ(SSLの設定等は記載省略)。
upstream nginxphp-upstream {
server 172.18.5.10:80;
keepalive 16;
}
server {
listen 443 ssl http2;
location /php {
proxy_pass http://nginxphp-upstream;
### proxy set header
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Forwarded-Proto https;
proxy_set_header X-Forwarded-SSL on;
proxy_set_header X-Forwarded-HTTPS on;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
}
}
AWSのALBやCloudFront経由時のheaderを確認するのに使えると思う。