amazon-web-services - 如何在具有 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;
}
}
解决方案
推荐阅读
- python - 如何使用 tf.gather_nd 从图像中进行选择?
- javascript - 在 Vue 中遍历分组的对象数据
- python - 使用 apply 从其他表中查找数据
- java - “在 JSP 中不推荐使用绝对路径”?- 这是否也适用于@RequestMapping()?
- regex - 在不将 XFST 替换规则应用于词典的情况下检测循环馈送交互
- react-redux - selectfromresult rtk 始终未定义
- python - 修改从“bing”解析dork链接的脚本的任何帮助
- for-loop - 如何在 Julia 中进行一定次数的迭代后停止 for 循环
- gcc - 将 C 程序编译为固定长度的 RISCV 指令
- python-3.x - 如何使用python将excel文件中的特定工作表转换为pdf