带有多个Laravel应用程序和AWS网关的Nginx

cygmwpex  于 2022-11-02  发布在  Nginx
关注(0)|答案(1)|浏览(183)

我在/var/www/html下的EC2示例上有3个Laravel应用程序:

  • /var/www/html/应用程序1
  • /var/www/html/应用程序2
  • /var/www/html/应用程序3

EC2示例(ubuntu服务器)没有公共IP,只能通过
1-API网关
2-当我使用VPN连接时,它的专用IP。
我创建了3个API网关路由,例如:

我目前的Nginx服务器配置如下

server {
       listen 80;
       root /var/www/html/app1/public;

        # Add index.php to the list if you are using PHP
        index index.html index.htm index.php;

        server_name _;

        add_header X-Frame-Options "SAMEORIGIN";
        add_header X-Content-Type-Options "nosniff";

        location / {
               # First attempt to serve request as file, then
               # as directory, then fall back to displaying a 404.
               try_files $uri $uri/ /index.php$is_args$args;
        }

        # pass PHP scripts to FastCGI server
        location ~ \.php$ {
                           fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
                           fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
                           include fastcgi_params;
        }

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #       deny all;
        #}
}

当我使用服务器的私有IP时(当VPN连接打开时),这似乎可以按预期工作,路由将被解析为10.0.143.153/login或10.0.143.153/users。
然而,当我使用“https://MyGatewayID.execute-api.us-west-2.amazonaws.com“时,它只打开索引页,但如果我试图在应用程序中按“登录”,它将试图导航到https://MyGatewayID.execute-api.us-west-2.amazonaws.com/login,这将无法工作。
我不知道如何使用多个应用程序,似乎我尝试的所有解决方案都使用一个应用程序,或者它们使用server_name变量,我认为这在我的情况下无关紧要(因为服务器没有公共IP)。
我尝试了这个解决方案
Nginx: Serve multiple Laravel apps with same url but two different sub locations in Linux
当VPN打开时,它似乎可以与private-ip/app 2 private-ip/app 3一起工作。但是,我不能使它与API网关一起工作。而且,这个解决方案需要在应用程序代码中进行一些修改,我想知道是否只能使用nginx config来完成。
我是完全新的Nginx和awsapi网关,所以请原谅我缺乏知识,并指导我正确的方向,如果你认为我错过了重点。

o2rvlv0m

o2rvlv0m1#

使用此答案中的解决方案
https://stackoverflow.com/a/63882428/3382285
AWS API网关路由需要与Nginx配置路由中的应用程序位置相匹配,并且需要使用相同的端口进行集成。
例如,如果位置为/app 2

location /app2 {
               # First attempt to serve request as file, then
               # as directory, then fall back to displaying a 404.
               try_files $uri $uri/ /index.php$is_args$args;
        }

则AWS网关将类似于https://MyGatewayID.execute-api.us-west-2.amazonaws.com/app2
为了使所有应用程序路线都能正常工作,我使用了一个通配符路线,例如:
/应用程序2/{应用程序接口+}
我不能100%确定使用这样的解决方案会对性能产生什么影响,因为所有的应用程序都在一个nginx服务器块中,并且使用相同的端口,但这将是另一个问题。

相关问题