首页 > 解决方案 > 如何在具有 SSL 终止的 AWS ELB 后面的多个 EC2 实例上使用 nginx 正确配置 Laravel 应用程序?

问题描述

我有一个旧版 Laravel 5.7 应用程序,带有我在 AWS 中设置的 vue.js 客户端。

该应用程序在此设置中运行良好:带有 nginx 反向代理的单个 EC2 实例,API 和客户端都在同一实例上运行。

此设置中存在身份验证问题:具有 ssl 终止的弹性负载均衡器后面的单个目标组中的多个 EC2 实例(具有每个实例的 nginx 反向代理)。对于客户端组和 API 组,此设置相同。

在负载平衡设置中,每次单击“登录”按钮时,都会看到对身份验证端点的重复调用。这导致每次登录尝试都会发出多个身份验证令牌。

每个身份验证请求的第一个登录后调用都会导致 HTTP 401 响应。它让我回到登录屏幕。 浏览器网络选项卡输出

我在 nginx 配置中为两个设置之间的 API 和客户端所做的更改被删除了这些行

    # Route all HTTP traffic to HTTPS
    server {
        listen      *:80;
        add_header  Strict-Transport-Security max-age=15768000;
        return      301 https://$host$request_uri;
    }

改变了

listen    *:443 ssl;

listen    *:80;

在服务器块中,并从服务器块中删除了 ssl_certificate 和 ssl_certificate_key 设置,因为 ssl 终止现在发生在 ELB。

API ELB 配置为将所有端口 80 请求路由到端口 443,负载平衡算法设置为“最少未完成请求”并关闭粘性。

客户端 ELB 还配置为将所有端口 80 请求路由到端口 443,负载平衡算法设置为“最少未完成请求”。但是,使用 lb 发布的 cookie 打开 Stickines。

似乎问题出在负载均衡器或 nginx 配置上,因为这是两个设置之间唯一发生变化的两件事。

Laravel 应用程序配置中是否需要更新某些内容才能在负载平衡场景中正常工作?vue.js 客户端应用程序呢?我的负载均衡器或目标组中是否有需要设置或更改的设置?还是需要在 nginx 配置中设置一些东西?

负载平衡 API 设置上的 Nginx 配置:

nginx.conf

user #redacted;
worker_processes auto;
pid /run/nginx.pid;

events {
    worker_connections 2048;
    multi_accept on;
    use epoll;
}

http {
    include     mime.types;
    default_type    application/octet-stream;

    server_tokens   off;

    #####################
    # Basic Settings
    #####################

    sendfile        on;
    keepalive_timeout   65;
    client_body_timeout 10;
    client_header_timeout   10;
    send_timeout        10;
    tcp_nopush      on;
    tcp_nodelay     on;
    gzip            on;
    charset         UTF-8;
    client_max_body_size    100M;

    ######################
    # Logging Settings
    ######################

    access_log      /var/log/nginx/access.log;
    error_log       /var/log/nginx/error.log;

    include /etc/nginx/sites-available/*.conf;  
}

site.conf(包含文件)

######################
# Server Settings
######################

server {
    listen          *:80;
    root            /app/public;
    index           index.php;

    location / {
        try_files $uri $uri/ /index.php$is_args$args;
    }
        location ~ \.php$ {
                try_files $uri = /404.html;
                fastcgi_pass 127.0.0.1:8080;
                fastcgi_index index.php;
                fastcgi_buffers 16 16k;
                fastcgi_buffer_size 32k;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                fastcgi_param PATH_INFO $fastcgi_path_info;
                fastcgi_read_timeout 300;
                include fastcgi_params;
    }
}

负载平衡客户端设置上的 Nginx 配置:

nginx.conf

user               #redacted;
worker_processes   auto;
pid                /run/nginx.pid;

events {
    worker_connections 2048;
    multi_accept on;
    use epoll;
}

http {
    include         mime.types;
    default_type    application/octet-stream;

    server_tokens   off;

    #####################
    # Basic Settings
    #####################

    sendfile        on;
    keepalive_timeout   65;
    client_body_timeout 10;
    client_header_timeout   10;
    send_timeout        10;
    tcp_nopush      on;
    tcp_nodelay     on;
    gzip            on;
    charset         UTF-8;
    client_max_body_size    100M;

    ######################
    # Logging Settings
    ######################

    access_log      /var/log/nginx/access.log;
    error_log       /var/log/nginx/error.log;

    include /etc/nginx/sites-available/*.conf;  
}

}

site.conf(包含文件)

server {
        listen                  *:80;
        server_name             #redacted;
        root                    /var/www/app;
                
        # Headers
        add_header              X-Frame-Options DENY;
                              
        location / {
                try_files $uri $uri/ /index.html;
                index index.htm index.html;
        }
}   

标签: amazon-web-servicesnginxssllaravel-5

解决方案


推荐阅读