You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

多Laravel应用搭配Nginx与AWS API网关的配置问题求助

解决方案:仅通过Nginx配置适配API网关与多Laravel应用

核心问题分析

你的问题本质是两个点:

  1. API网关转发请求时,Laravel无法识别真实的请求协议(HTTPS)和域名,导致登录跳转URL生成错误
  2. app2、app3的请求带有/app2//app3前缀,Laravel路由无法匹配这些带前缀的路径

以下是无需修改Laravel代码的纯Nginx配置方案:


步骤1:修改Nginx站点配置

编辑你的Nginx主配置文件(通常是/etc/nginx/sites-available/default),替换为以下内容:

server {
    listen 80;
    server_name _; # 匹配所有来自API网关/VPN的请求

    # 处理app1(根路径)
    location / {
        root /var/www/html/app1/public;
        try_files $uri $uri/ /index.php?$query_string;

        # 静态文件缓存优化
        location ~* \.(css|js|png|jpg|jpeg|gif|ico|svg)$ {
            expires 1y;
            add_header Cache-Control "public, immutable";
        }

        # PHP请求处理
        location ~ \.php$ {
            include snippets/fastcgi-php.conf;
            fastcgi_pass unix:/run/php/php8.2-fpm.sock; # 根据你的PHP版本调整,比如php8.1-fpm.sock
            # 传递API网关转发头,让Laravel生成正确的HTTPS URL
            fastcgi_param HTTP_X_FORWARDED_PROTO $http_x_forwarded_proto;
            fastcgi_param HTTP_X_FORWARDED_HOST $http_x_forwarded_host;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        }
    }

    # 处理app2(/app2路径)
    location /app2/ {
        # 剥离/app2前缀,将请求转发到app2的public目录
        rewrite ^/app2/(.*)$ /$1 break;
        root /var/www/html/app2/public;
        try_files $uri $uri/ /index.php?$query_string;

        # 静态文件缓存优化
        location ~* \.(css|js|png|jpg|jpeg|gif|ico|svg)$ {
            expires 1y;
            add_header Cache-Control "public, immutable";
        }

        # PHP请求处理
        location ~ \.php$ {
            include snippets/fastcgi-php.conf;
            fastcgi_pass unix:/run/php/php8.2-fpm.sock;
            fastcgi_param HTTP_X_FORWARDED_PROTO $http_x_forwarded_proto;
            fastcgi_param HTTP_X_FORWARDED_HOST $http_x_forwarded_host;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        }
    }

    # 处理app3(/app3路径)
    location /app3/ {
        rewrite ^/app3/(.*)$ /$1 break;
        root /var/www/html/app3/public;
        try_files $uri $uri/ /index.php?$query_string;

        # 静态文件缓存优化
        location ~* \.(css|js|png|jpg|jpeg|gif|ico|svg)$ {
            expires 1y;
            add_header Cache-Control "public, immutable";
        }

        # PHP请求处理
        location ~ \.php$ {
            include snippets/fastcgi-php.conf;
            fastcgi_pass unix:/run/php/php8.2-fpm.sock;
            fastcgi_param HTTP_X_FORWARDED_PROTO $http_x_forwarded_proto;
            fastcgi_param HTTP_X_FORWARDED_HOST $http_x_forwarded_host;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        }
    }
}

步骤2:验证配置并重启Nginx

  1. 检查配置语法是否正确:
sudo nginx -t
  1. 若提示test is successful,重启Nginx生效:
sudo systemctl restart nginx

关键配置说明

  • rewrite ^/app2/(.*)$ /$1 break;:自动剥离/app2前缀,让Laravel路由能匹配真实的路径(比如把/app2/login转成/login交给app2处理)
  • fastcgi_param相关配置:传递API网关的HTTPS协议和域名信息,Laravel会自动用这些信息生成正确的跳转URL(比如登录跳转的https://MyGatewayID.execute-api.us-west-2.amazonaws.com/login)
  • server_name _;:适配无公网IP的场景,匹配所有来自API网关和VPN的请求

测试验证

  1. 访问API网关的app1地址,点击登录按钮,跳转应正常工作
  2. 测试app2和app3的API网关地址,验证首页、跳转及功能是否正常

内容的提问来源于stack exchange,提问作者Muhannad

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.15 00:35:26