【问题标题】:How to set HTTPS as default on nginx如何在 nginx 上将 HTTPS 设置为默认值
【发布时间】:2016-01-12 15:01:51
【问题描述】:

我有工作启用 SSL 的域、服务器,并且默认情况下它已启动并运行 HTTP。当我请求https://domain.tld 时,它可以正常工作,但是如何将其设为默认而不是HTTP

现在我的 nginx vhost 有以下指令

server {

    listen 80 ;
    listen 443 ssl default_server;
    ssl on ;
    ssl_certificate /etc/nginx/ssl/XX.crt;
    ssl_certificate_key /etc/nginx/ssl/XX.key;

    server_name x.me www.x.me;
    root /usr/share/nginx/www/x/site;
    index index.html index.php;

    location / {
            try_files $uri $uri/ /index.php?q=$request_uri;
    }

    location ~ \.php$ {
            try_files $uri =404;
            fastcgi_pass unix:/var/run/php5-fpm.sock;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include fastcgi_params;
    }

}

【问题讨论】:

    标签: ssl nginx https


    【解决方案1】:

    将 http 请求重定向到 https。这可以通过脚本来完成,也可以通过使用 'return' 的 nginx 来完成。

    教程:https://christiaanconover.com/blog/how-to-redirect-http-to-https-in-nginx

    【讨论】:

      【解决方案2】:

      添加一个单独的服务器,它将重定向到 https:

      server {
          listen 80;
          server_name x.me www.x.me;
          return 301 https://$host$request_uri;
      }
      

      并从现有代码块中删除 listen 80

      【讨论】:

      • 放在哪里?它会放在我现有的上面块吗?
      • 是的,将'return'指令添加到您的服务器块中。
      • Chrome 返回我 ERR_TOO_MANY_REDIRECTS ,我当前的服务器块pastebin.com/HW1A3XVf
      猜你喜欢
      • 2020-11-29
      • 2020-07-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-15
      • 2011-07-08
      相关资源
      最近更新 更多