CentOS 8环境下Nginx单域名映射多个Lumen项目的配置问题求助
问题原因
location /search错误使用root指令:root会将请求URI完整拼接在根路径后,比如请求/search/test会被映射到/var/www/domain.com/html/api/search/public/search/test,该路径不存在直接触发404。- Nginx匹配优先级问题:正则匹配的
location ~ \.php$优先级高于普通前缀匹配的location /search,所有PHP请求会直接走外层PHP处理块,使用全局配置的gateway项目根路径,完全不会读取search项目的文件,要么路径不存在返回404,要么权限不匹配返回403。 - Lumen路由前缀未处理:/search路径下的请求到达Lumen时会携带
/search前缀,Lumen会将其作为路由的一部分,找不到对应路由返回404。
修复方案
1. 调整Nginx配置
将原注释的/search配置替换为以下内容,其他全局配置无需修改,剩余5个Lumen项目可按照相同格式新增对应location ^~ /项目名块即可:
server { root /var/www/domain.com/html/api/gateway/public; index index.php index.html index.htm index.nginx-debian.html; server_name domain.com www.domain.com; location / { autoindex on; try_files $uri $uri/ /index.php?$query_string; } # search项目配置 location ^~ /search { alias /var/www/domain.com/html/api/search/public; index index.php index.html index.htm; try_files $uri $uri/ /search/index.php?$query_string; # 单独处理当前项目下的PHP请求 location ~ \.php$ { fastcgi_split_path_info ^(.+\.php)(/.+)$; fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $request_filename; include fastcgi_params; } } location ~ \.php$ { autoindex on; autoindex_exact_size on; fastcgi_split_path_info ^(.+\.php)(/.+)$; fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } # 以下SSL、80端口跳转配置保持不变 listen [::]:443 ssl ipv6only=on; # managed by Certbot listen 443 ssl; # managed by Certbot ssl_certificate /etc/letsencrypt/live/domain.com/fullchain.pem; # managed by Certbot ssl_certificate_key /etc/letsencrypt/live/domain.com/privkey.pem; # managed by Certbot include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot } server { if ($host = domain.com) { return 301 https://$host$request_uri; } # managed by Certbot listen 80; listen [::]:80; server_name domain.com www.domain.com; return 404; # managed by Certbot }
2. 配置生效
- 执行
nginx -t检查配置语法,返回success代表配置无错误。 - 执行
systemctl reload nginx重载配置使其生效。
3. 权限校验
确认所有Lumen项目的根目录,以及storage、bootstrap/cache目录对Nginx运行用户、php-fpm运行用户有读写权限,避免出现403错误。
内容的提问来源于stack exchange,提问作者Vicky Gill
相关产品推荐
相关产品推荐

