【问题标题】:AWS ECS & Nginx - Change from Http to Https is not workingAWS ECS 和 Nginx - 从 Http 更改为 Https 不起作用
【发布时间】:2021-02-26 03:59:28
【问题描述】:

您好,我部署了我的网站。一切正常。然后我从 http 更改为 https,现在我得到一个空白页或 502 502 Bad Gateway。我认为问题出在我的 nginx.conf 中。

我在 AWS ESC 上的同一服务中的同一任务中部署我的前端和后端。

这是我的端口:

Http->80
Https->443
Client Port 8080
Backend Port 4000

这是我在更改为 https 之前的 nginx.conf(有效):

worker_processes auto;

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


http {
    sendfile        on;
    tcp_nopush      on;
    tcp_nodelay     off;
    gzip on;
    gzip_http_version  1.0;
    gzip_comp_level    5;
    gzip_min_length    256;
    gzip_proxied       any;
    gzip_vary          on;
    gzip_types
        application/atom+xml
        application/javascript
        application/json
        application/rss+xml
        application/vnd.ms-fontobject
        application/x-font-ttf
        application/x-web-app-manifest+json
        application/xhtml+xml
        application/xml
        font/opentype
        image/svg+xml
        image/x-icon
        text/css
        text/plain
        text/x-component;

    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format compression '$remote_addr - $remote_user [$time_local] '
        '"$request" $status $upstream_addr '
        '"$http_referer" "$http_user_agent" "$gzip_ratio"';

    server {
        listen 8080;
        server_name mydomain.com;
        access_log /var/log/nginx/access.log compression;
                
        root /usr/share/nginx/html;
        index index.html index.htm;

        location ~* \.(?:manifest|appcache|html?|xml|json)$ {
            expires -1;
        }

        location / {
            try_files $uri $uri/ /index.html;
        }

        location /graphql {
            proxy_pass http://localhost:4000/graphql;
        }

        location /subscriptions {
            proxy_pass http://localhost:4000/subscriptions;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
        }

        location /refresh_token {
            proxy_pass http://localhost:4000/refresh_token;
            proxy_set_header Authorization $http_authorization;
            proxy_pass_header Authorization;
        }

        location ~* \.(?:jpg|jpeg|gif|png|ico|cur|gz|svg|svgz|mp4|ogg|ogv|webm|htc)$ {
            expires 1M;
            access_log off;
            add_header Cache-Control "public";
        }

        location ~* \.(?:css|js)$ {
            try_files $uri =404;
            expires 1y;
            access_log off;
            add_header Cache-Control "public";
        }

        location ~ ^.+\..+$ {
            try_files $uri =404;
        }

        location /static/ {
            root /var/www;
        }
    }
}

我对我的 nginx.conf 做了很多更改,但没有任何效果。我使用 AWS 验证了我的域。 这里我做了一些改变:

server {
        listen 80;
        listen [::]:80;
        server_name mydomian.com
        return 301 https://$server_name$request_uri;
    }

    server {
        listen 443;
        listen [::]:443;
        server_name mydomian.com
        access_log /var/log/nginx/access.log compression;
                
        root /usr/share/nginx/html;
        index index.html index.htm;

        location / {
            proxy_pass http://localhost:8080;
            try_files $uri $uri/ /index.html;
        }
    }

我正在使用负载平衡器来终止 SSL。

【问题讨论】:

  • 您是否已经在使用 AWS 负载均衡器?如果是这样,在此处终止 SSL 会更容易,因此您不必在 ECS 服务中处理它。
  • 是的,我做到了。如果我访问域,它是经过验证的 https。

标签: amazon-web-services nginx


【解决方案1】:

我专注于 nginx.conf,但 conf 很好。我正在使用导致问题的 websocket:

const host = window.location.host;
`ws://${host}/subscriptions`

so I added a s: 
`wss://${host}/subscriptions`

两天来,我尝试了 nginx.conf 中的所有内容,我只需要添加一个 s。 我超级愚蠢,因为我没有检查控制台是否有错误。

【讨论】:

    猜你喜欢
    • 2012-08-13
    • 2018-03-27
    • 2022-01-14
    • 1970-01-01
    • 2021-11-13
    • 1970-01-01
    • 2014-08-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多