【问题标题】:nginx http to https redirect configuration not workingnginx http到https重定向配置不起作用
【发布时间】:2018-05-16 17:48:49
【问题描述】:

我已经根据提供的文档和网络上的文章配置了我的 nginx。它并不完全适用于 http 到 https。 我尝试了不同的更改,但仍然无法成功执行...请看一下。

几个小鬼点:我的 . nodejs 应用程序在端口 3000 上运行。 Ghost 博客在 2368 上运行。

HTTP — redirect all traffic to HTTPS
server {
    listen 80;
    server_name domainname.com www.domainname.com;
    return 301 https://$server_name$request_uri;
}

server {
    listen 443 ssl http2 default_server;
    listen [::]:443 ssl http2 default_server;
    server_name www.domainname.com;
    error_page 497 https://www.domainname.com$request_uri;

    ssl_certificate /etc/letsencrypt/live/domainname.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/domainname.com/privkey.pem;
    ssl_session_timeout 1d;
    ssl_session_cache shared:SSL:50m;
    ssl_session_tickets off;

    # intermediate configuration. tweak to your needs.
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers KEY_HERE;
    ssl_prefer_server_ciphers on;

    # HSTS (ngx_http_headers_module is required) (15768000 seconds = 6 months)
    add_header Strict-Transport-Security max-age=15768000;
   # OCSP Stapling ---
    # fetch OCSP records from URL in ssl_certificate and cache them
    ssl_stapling on;
    ssl_stapling_verify on;

    ## verify chain of trust of OCSP response using Root CA and Intermediate certs
    # ssl_trusted_certificate /etc/ssl/certs/dhparam.pem;

    resolver 8.8.8.8;

    location / {
        proxy_pass http://localhost:3000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }

    location /blog {
        proxy_pass http://localhost:2368;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }
}

【问题讨论】:

    标签: nginx nginx-reverse-proxy


    【解决方案1】:

    此问题已解决。

    nginx 配置一切正常。问题出在 Google 控制台平台上。 GCP 配置中有一个名为 Allow HTTP traffic 的复选框,默认情况下未选中。我做了改变,它开始工作了。感谢您的回复。

    【讨论】:

      【解决方案2】:

      我建议你这样做:

      location / {
          return 301 https://$host$request_uri;
      }
      

      【讨论】:

      • 我尝试了这个并在'location / {}`中保留了其他行,因为我想使用proxy_pass等......在}之前添加了这一行......它没有工作...... .让我知道这是不是问题..谢谢
      • 我想您希望我将此代码放在服务器块而不是位置...请确认..
      • return works inside location,我觉得你还有问题,你可以发送一些日志。
      猜你喜欢
      • 2022-01-14
      • 1970-01-01
      • 2014-08-14
      • 2017-02-06
      • 2018-03-27
      • 1970-01-01
      • 1970-01-01
      • 2014-03-10
      • 2018-06-11
      相关资源
      最近更新 更多