ssl - 使用卸载到 ELB 的 SSL 解密在 AWS EC2 nginx 上的重定向中添加的无效端口

标签 ssl nginx amazon-ec2 amazon-elb

在 AWS 上,我正在尝试迁移在 nginx 上运行的 PHP Symfony 应用程序。我希望能够通过直接与 EC2 服务器对话并通过 ELB(公共(public)路由)来测试应用程序。

我已经设置了一个弹性负载均衡器来解密所有 SSL 流量并通过端口 80 将其传递到我的 EC2 服务器,以及通过端口 80 将端口 80 直接传递到我的 EC2 服务器。

最初这导致了我的应用程序中的无限重定向,但我研究并通过添加修复了这个问题

fastcgi_param HTTPS $https;

使用一些自定义逻辑查看 $http_x_forwarded_proto 以确定它何时实际通过 SSL。

还有一个问题我无法解决。当用户登录到 Symfony 应用程序时,如果他们通过 ELB 访问,则 POST 表单最终会返回一个重定向到 https://elb.mysite.com:80/dashboard 代替 https://elb.mysite.com/dashboard 这给用户一个“SSL 连接错误”的错误。

我试过设置

fastcgi_param SERVER_PORT $fastcgi_port; 

迫使它远离 80,我还添加了

port_in_redirect off

指令,但两者没有区别。

我发现解决此问题的唯一方法是更改​​ ELB 443 监听器以通过 https 传递流量。 EC2 服务器配置了自认证的 SSL 证书。但这意味着 EC2 服务器正在浪费容量来执行这种不必要的第二次解密。

非常感谢任何帮助。也许 nginx 中有一种单独的方法告诉 POST 请求不要应用端口号?

Nginx 虚拟主机配置:

server {
        port_in_redirect off;

        listen 80;
        listen 443 ssl;

        ssl_certificate /etc/nginx/ssl/mysite.com/self-ssl.crt;
        ssl_certificate_key /etc/nginx/ssl/mysite.com/self-ssl.key;

        # Determine if HTTPS being used either locally or via ELB
        set $fastcgi_https off;
        set $fastcgi_port 80;
        if ( $http_x_forwarded_proto = 'https' ) {
          # ELB is using https
          set $fastcgi_https on;
#          set $fastcgi_port 443;
        }
        if ( $https = 'on' ) {
          # Local connection is using https
          set $fastcgi_https on;
#          set $fastcgi_port 443;
        }

        server_name *.mysite.com my-mysite-com-1234.eu-west-1.elb.amazonaws.com;

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

        rewrite ^/app\.php/?(.*)$ /$1 permanent;

        location / {
                port_in_redirect off;
                root /var/www/vhosts/mysite.com/web;
                index app.php index.php index.html index.html;
                try_files $uri @rewriteapp;
        }

        location ~* \.(jpg|jpeg|gif|png)$ {
               root /var/www/vhosts/mysite.com/web;
               access_log off;
               log_not_found off;
               expires 30d;
        }

        location ~* \.(css|js)$ {
                root /var/www/vhosts/mysite.com/web;
                access_log off;
                log_not_found off;
                expires 2h;
        }

        location @rewriteapp {
           rewrite ^(.*)$ /app.php/$1 last;
        }

        location ~ ^/(app|app_dev|config)\.php(/|$) {
                port_in_redirect off;
                fastcgi_pass   127.0.0.1:9000;
                fastcgi_split_path_info ^(.+\.php)(/.*)$;
                fastcgi_param HTTPS $fastcgi_https;
#                fastcgi_param SERVER_PORT $fastcgi_port;
                #fastcgi_index  index.php;
                fastcgi_param  SCRIPT_FILENAME  /var/www/vhosts/mysite.com/web$fastcgi_script_name;
                include fastcgi_params;
        }
}

引用资料: FastCGI application behind NGINX is unable to detect that HTTPS secure connection is used

https://serverfault.com/questions/256191/getting-correct-server-port-to-php-fpm-through-nginx-and-varnish

http://nginx.org/en/docs/http/ngx_http_core_module.html#port_in_redirect

最佳答案

终于通过其他 channel 得到了解决方案。

答案是在fastcgi_params文件中用#注释掉SERVER_PORT。

非常感谢 Nginx 的 Maxim。

关于ssl - 使用卸载到 ELB 的 SSL 解密在 AWS EC2 nginx 上的重定向中添加的无效端口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23959947/

相关文章:

nginx - 加载共享库时出错 : 'libpcre.so.0: cannot open shared object file: No such file or directory'

ssl - Nginx;如何使用OCSP验证SSL客户端证书

node.js - nginx 响应 "301 moved permanently"

amazon-ec2 - 连接到我正在运行的 Amazon EC2 实例被拒绝

amazon-ec2 - 亚马逊网络服务 : Different between Images and Instances

自添加 https 端点后,azure 计算模拟器未启动

java - JAVA 6 中出现 SSL 异常,但 JAVA 8 中没有 SSL 异常

amazon-web-services - HAProxy 和 AWS loadBalancer - 503 错误

docker - 在 Docker 中使用 KeyCloak 设置集成测试环境

Python 警告过滤器未捕获 InsecurePlatformWarning