Docker容器中HTML表单通过PHP提交数据遇405错误的解决方法
解决Docker容器中PHP表单提交405 Not Allowed错误
我有一个HTML表单,点击“提交”按钮会将输入数据POST到form.php,该PHP文件会接收数据并存储到data.json中。本地用php -S localhost:4040运行时功能正常,但用Docker容器部署后,点击提交出现405 Not Allowed错误。以下是我的代码片段:
原代码片段
HTML
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title> Registration</title> <link rel="stylesheet" href="css/styles.css"> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.3.1/dist/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous"> <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script> </head> <body class="container"> <div class="container"> <h1>Registration</h1> <section> <form id="regForm" action="form.php" method="post"> <div class="form-group"> <label for="nameInput">Name</label> <input type="text" class="form-control" id="nameInput" name="studentName" aria-describedby="emailHelp" placeholder="Enter your full name"> </div> <div class="form-group"> <label for="loginInput">Login</label> <input type="text" class="form-control" id="loginInput" name="studentId" placeholder="Enter your login"> </div> <button type="submit" name="submit" value="submit" class="btn btn-primary" id="submitForm">Submit</button> </form> </section> </div> <script src="https://cdn.jsdelivr.net/npm/popper.js@1.14.7/dist/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script> <script src="https://cdn.jsdelivr.net/npm/bootstrap@4.3.1/dist/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script> </body> </html>
PHP(form.php)
<?php error_reporting(0); echo "<h2>Submitted</h2>"; if(isset($_REQUEST['submit'])) { $data = ''; $filename = "data.json"; //if file exists if(is_file($filename)) { $data = file_get_contents($filename); } $json_arr = json_decode($data,true); $json_arr[] = array( 'studentId' => $_REQUEST['studentId'], 'studentName' => $_REQUEST['studentName'], ); //saving file in jsonarray file_put_contents($filename, json_encode($json_arr)); } ?>
原docker-compose.yml
version: '3' services: product-services: build: ./src volumes: - ./src:/usr/share/nginx/html ports: - 5001:80
问题原因
当前使用的是Nginx基础镜像,但Nginx默认无法解析PHP文件,当表单POST请求到form.php时,Nginx直接返回405错误,因为它不知道如何处理PHP的POST请求。而本地用php -S是PHP内置的Apache服务器,天然支持PHP解析。
解决方案
方案1:使用PHP官方Apache镜像(最简单)
直接更换基础镜像为PHP官方的Apache版本,无需额外配置即可支持PHP解析。
修改后的docker-compose.yml:
version: '3' services: product-services: image: php:8.2-apache volumes: - ./src:/var/www/html ports: - 5001:80
注意:PHP Apache镜像的网站根目录是
/var/www/html,而非Nginx的/usr/share/nginx/html,所以需要调整挂载路径。
方案2:配置Nginx支持PHP(适合坚持用Nginx的场景)
如果一定要用Nginx,需要搭建Nginx+PHP-FPM的组合,步骤如下:
- 在
./src目录下创建Dockerfile:
FROM nginx:alpine # 安装PHP-FPM RUN apk add --no-cache php82 php82-fpm php82-json # 替换Nginx默认配置 COPY nginx.conf /etc/nginx/conf.d/default.conf
- 创建
nginx.conf配置文件:
server { listen 80; root /usr/share/nginx/html; index index.html index.php; location / { try_files $uri $uri/ =404; } location ~ \.php$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } }
- 修改
docker-compose.yml:
version: '3' services: product-services: build: ./src volumes: - ./src:/usr/share/nginx/html ports: - 5001:80 command: sh -c "php-fpm82 && nginx -g 'daemon off;'"
额外注意:文件写入权限
无论用哪种方案,PHP需要有写入data.json的权限。可以在本地执行以下命令调整权限:
chmod 777 ./src/data.json # 或者如果文件还没创建,先创建再赋权 touch ./src/data.json && chmod 777 ./src/data.json
或者在Dockerfile中添加权限设置(更安全):
# 针对PHP Apache镜像的Dockerfile FROM php:8.2-apache RUN chown -R www-data:www-data /var/www/html
验证运行
修改配置后,执行以下命令启动容器:
docker-compose up -d
访问http://localhost:5001,提交表单即可正常存储数据到data.json。
内容的提问来源于stack exchange,提问作者James
相关产品推荐
相关产品推荐

