Angular+Lumen+Nginx环境POST请求遇405 Not Allowed错误求助
405 Not Allowed 错误排查方案(Angular+Lumen+Nginx部署场景)
问题背景
在AWS Ubuntu EC2实例上部署Angular前端+Lumen后端+Nginx架构的网站,向/api/auth/login发送POST请求时返回「405 Not Allowed」错误。该功能本地运行正常,数据库权限已确认无误,/api/auth/login路由未添加认证中间件,此前曾正常运行,修改Nginx配置后失效。
当前Nginx配置
server { listen 80; listen [::]:80; server_name ~~~~~~~~.compute-1.amazonaws.com; server_tokens off; client_max_body_size 10M; location = /assets/favicon.ico { access_log off; log_not_found off; } # The front end Angular location / { root /var/www/html; index index.html; try_files $uri $uri/ /index.html =404; } location /files/ { root /var/www/html/files; } # The lumen API location /api/ { error_log /var/log/nginx/error.log debug; root /var/www/html/api/public; try_files $uri $uri/ /index.php?$query_string; location ~ \.php$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/var/run/php/php8.2-fpm.sock; fastcgi_param SCRIPT_FILENAME $request_filename; } } }
注:为排查问题已关闭安全头,暂未配置SSL证书
Nginx错误日志(POST请求时)
2023/02/25 00:59:24 [debug] 371832#371832: *8 http cl:181 max:10485760 2023/02/25 00:59:24 [debug] 371832#371832: *8 rewrite phase: 3 2023/02/25 00:59:24 [debug] 371832#371832: *8 post rewrite phase: 4 2023/02/25 00:59:24 [debug] 371832#371832: *8 generic phase: 5 2023/02/25 00:59:24 [debug] 371832#371832: *8 generic phase: 6 2023/02/25 00:59:24 [debug] 371832#371832: *8 generic phase: 7 2023/02/25 00:59:24 [debug] 371832#371832: *8 access phase: 8 2023/02/25 00:59:24 [debug] 371832#371832: *8 access phase: 9 2023/02/25 00:59:24 [debug] 371832#371832: *8 access phase: 10 2023/02/25 00:59:24 [debug] 371832#371832: *8 post access phase: 11 2023/02/25 00:59:24 [debug] 371832#371832: *8 generic phase: 12 2023/02/25 00:59:24 [debug] 371832#371832: *8 try files handler 2023/02/25 00:59:24 [debug] 371832#371832: *8 http script var: "/api/auth/login" 2023/02/25 00:59:24 [debug] 371832#371832: *8 trying to use file: "/api/auth/login" "/var/www/html/api/public/api/auth/login" 2023/02/25 00:59:24 [debug] 371832#371832: *8 http script var: "/api/auth/login" 2023/02/25 00:59:24 [debug] 371832#371832: *8 trying to use dir: "/api/auth/login" "/var/www/html/api/public/api/auth/login" 2023/02/25 00:59:24 [debug] 371832#371832: *8 http script copy: "/index.php?" 2023/02/25 00:59:24 [debug] 371832#371832: *8 trying to use file: "/index.php?" "/var/www/html/api/public/index.php?" 2023/02/25 00:59:24 [debug] 371832#371832: *8 internal redirect: "/index.php?"
排查解决思路
1. 修正Nginx API路由的重定向参数
从日志可见,Nginx内部重定向到/index.php?时丢失了原请求的URI路径,导致Lumen无法正确解析路由。调整try_files和FastCGI参数:
location /api/ { error_log /var/log/nginx/error.log debug; root /var/www/html/api/public; try_files $uri $uri/ /index.php$is_args$args; location ~ \.php$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/var/run/php/php8.2-fpm.sock; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param REQUEST_URI $request_uri; } }
$is_args$args:保留原请求的查询参数,避免参数丢失$document_root$fastcgi_script_name:确保PHP-FPM能正确找到index.php文件REQUEST_URI:传递完整的请求路径给Lumen,让路由系统能匹配到对应的POST接口
修改后重启Nginx:
sudo systemctl restart nginx
2. 检查Lumen路由定义
确认routes/web.php中/api/auth/login的POST路由是否正确定义:
$router->post('/auth/login', 'AuthController@login');
同时检查全局中间件是否存在限制请求方法的逻辑,确保POST方法未被拦截。
3. 验证PHP-FPM的权限与配置
检查php8.2-fpm.sock的权限,确保Nginx进程用户(默认www-data)拥有读写权限:
ls -l /var/run/php/php8.2-fpm.sock
若权限不符,修改/etc/php/8.2/fpm/pool.d/www.conf中的listen.owner和listen.group为www-data,然后重启PHP-FPM:
sudo systemctl restart php8.2-fpm
4. 排查Nginx的请求方法限制
检查Nginx配置中是否存在limit_except指令限制POST方法,若有类似以下配置需移除:
limit_except GET HEAD { deny all; }
5. 直接在服务器端测试API
用curl在EC2实例内部直接发送POST请求,排除前端跨域或请求格式问题:
curl -X POST -H "Content-Type: application/json" -d '{"email":"test@example.com","password":"test123"}' http://localhost/api/auth/login
若仍返回405,查看Lumen的storage/logs目录下的日志文件,确认是否存在路由匹配错误或控制器逻辑问题。
内容的提问来源于stack exchange,提问作者misterz
相关产品推荐
相关产品推荐

