NGINX 在应用程序 AWS 负载均衡器后面使用 SSL 提供 http 文件,应为 https

NGINX 在应用程序 AWS 负载均衡器后面使用 SSL 提供 http 文件,应为 https

我对此完全陌生,所以请对我宽容一点。我有一个网站,当通过浏览器访问时,它是在 https 上。在我的管理区域中,我有一个 wysiwyg (TinyMCE) 并使用 MoxieManager 插件。当我选择一张图片时,它会显示为http://mydomain(dot)com/image.jpg当它应该是相对路径时。

在 AWS 中,您可以看到在应用程序负载均衡器设置中,我将 HTTP:80 重定向到 HTTPS:443。

负载均衡器设置图像

对于我的 NGINX 设置,我有以下内容:

fastcgi_cache_path /etc/nginx/cache levels=1:2 keys_zone=phpcache:100m inactive=60m;
fastcgi_cache_key "$scheme$request_method$host$request_uri";

# Expires map
map $sent_http_content_type $expires {
    default                    off;
    text/html                  epoch;
    text/css                   max;
    application/javascript     max;
    ~image/                    max;
}

server {
    listen 80 default_server;
    listen [::]:80 default_server;

    listen 443 ssl http2 default_server;
    listen [::]:443 ssl http2 default_server;

    server_name mydomain.com;

    root /home/forge/mydomain.com/public;

    expires $expires; 

    # exposure of server name ex. nginx instead of nginx/1.13.3 
    server_tokens off;

    # FORGE SSL (DO NOT REMOVE!)
    # ssl_certificate;
    # ssl_certificate_key;

    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers 'ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-DSS-AES128-GCM-SHA256:kEDH+AESGCM:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA:DHE-DSS-AES128-SHA256:DHE-RSA-AES256-SHA256:DHE-DSS-AES256-SHA:DHE-RSA-AES256-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:AES128-SHA256:AES256-SHA256:AES128-SHA:AES256-SHA:AES:CAMELLIA:DES-CBC3-SHA:!aNULL:!eNULL:!EXPORT:!DES:!RC4:!MD5:!PSK:!aECDH:!EDH-DSS-DES-CBC3-SHA:!EDH-RSA-DES-CBC3-SHA:!KRB5-DES-CBC3-SHA:!3DES';
    ssl_prefer_server_ciphers on;
    ssl_dhparam /etc/nginx/dhparams.pem;

    add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload";
    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-XSS-Protection "1; mode=block"; 
    add_header X-Content-Type-Options "nosniff";
    add_header Referrer-Policy "origin";

    client_max_body_size 200M;

    index index.html index.htm index.php;

    charset utf-8;

    # FORGE CONFIG (DOT NOT REMOVE!)
    include forge-conf/mydomain.com/server/*;

    # Get users true ip address
    set_real_ip_from 10.0.0.0/16;
    real_ip_header X-Forwarded-For;

    location / {   
        # exposure of server name ex. nginx instead of nginx/1.13.3 
        server_tokens off;

        # Redirect HTTP to HTTPS
        if ($http_x_forwarded_proto != "https") {
        return 301 https://$server_name$request_uri;
        }

        try_files $uri $uri/ /index.php?$query_string;
    }
    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }

    access_log off;
    error_log  /var/log/nginx/mydomain.com-error.log error;

    error_page 404 /index.php;

    location ~ \.php$ {

        fastcgi_cache phpcache; # The name of the cache key-zone to use
        fastcgi_cache_valid 200 30m; # What to cache: 'Code 200' responses, for half an hour
        fastcgi_cache_methods GET HEAD; # What to cache: only GET and HEAD requests (not POST)
        add_header X-Fastcgi-Cache $upstream_cache_status; # Add header so we can see if the cache hits or misses

        # try_files $uri 404;
        try_files $uri /index.php =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php/php7.1-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;

    }

    location ~ /\.(?!well-known).* {
        deny all;
    }

    # Enable gzip compression.
    # Default: off
    gzip on;

    # Compression level (1-9).
    # 5 is a perfect compromise between size and CPU usage, offering about
    # 75% reduction for most ASCII files (almost identical to level 9).
    # Default: 1
    gzip_comp_level    5;

    # Don't compress anything that's already small and unlikely to shrink much
    # if at all (the default is 20 bytes, which is bad as that usually leads to
    # larger files after gzipping).
    # Default: 20
    gzip_min_length    256;

    # Compress data even for clients that are connecting to us via proxies,
    # identified by the "Via" header (required for CloudFront).
    # Default: off
    gzip_proxied       any;

    # Tell proxies to cache both the gzipped and regular version of a resource
    # whenever the client's Accept-Encoding capabilities header varies;
    # Avoids the issue where a non-gzip capable client (which is extremely rare
    # today) would display gibberish if their proxy gave them the gzipped version.
    # Default: off
    gzip_vary          on;

    # Compress all output labeled with one of the following MIME-types.
    # text/html is always compressed by gzip module.
    # Default: text/html
    gzip_types
        application/atom+xml
        application/javascript
        application/json
        application/ld+json
        application/manifest+json
        application/rss+xml
        application/vnd.geo+json
        application/vnd.ms-fontobject
        application/x-font-ttf
        application/x-web-app-manifest+json
        application/xhtml+xml
        application/xml
        font/opentype
        image/bmp
        image/svg+xml
        image/x-icon
        text/cache-manifest
        text/css
        text/plain
        text/vcard
        text/vnd.rim.location.xloc
        text/vtt
        text/x-component
        text/x-cross-domain-policy;
}

在 moxiemanager 配置中,url 设置为相对,没有协议或域。但是,当我在 Moxiemanager 中选择图像时,我会获得完整的 url、协议调用、域和路径。

在浏览器控制台中,它清楚地表明 MoxieManager 图像是通过 http 而不是 https 加载的。见下文:

Google Chrome 控制台图片

我认为问题出在我的 NGINX 设置上。我之所以这么想,是因为我有第二个网站,它使用完全相同的代码,也是 https,但 SSL 是通过 Laravel Forge 设置的,它不使用负载平衡器。当我在第二个网站上的 Moxiemanager 中选择图像时,它是相对的,一切都按预期运行。所以我不认为 Moxiemanager 配置有误,我也与他们的支持团队核实了这一点。

有谁知道我是否走在正确的道路上或看到了一个显而易见的解决方案?至少 6 个月以来,我搜索了互联网并尝试了大量不同的 NGINX 配置,但都没有成功。任何帮助都将不胜感激!

相关内容