Nginx重定向https://非www到http:// www无效

时间:2017-09-11 07:37:55

标签: ssl redirect nginx

我花了一点时间寻找答案,但似乎无法将其锁定,任何帮助都会受到赞赏。

我在Nginx服务器上设置了SSL,但似乎无法让非www https域重定向到www https域。

我想解决这个问题:

  

https://example.com - > https://www.example.com

以下是我当前的配置:

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

server {

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

    server_name example.com www.example.com;

    root /var/www/site;

    index index.php index.html index.htm;

    client_max_body_size 10m;

    # unless the request is for a valid file, send to bootstrap
    if (!-e $request_filename)
    {
        rewrite ^(.+)$ /index.php?q=$1 last;
    }

    # catch all
    error_page 404 /index.php;

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    location ~ \.php$ {
        fastcgi_pass   unix:/var/run/php5-fpm.sock;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  /var/www/site$fastcgi_script_name;
        include        fastcgi_params;
    }

    # deny access to apache .htaccess files
    location ~ /\.ht
    {
        deny all;
    }


  location = /xmlrpc.php {
      deny all;
  }

    location ~* \.html$ {
        expires -1;
    }

  location ~ \.(htc|less|js|js2|js3|js4)$ {
      expires 31536000s;
      add_header Pragma "public";
      add_header Cache-Control "max-age=31536000, public";
      try_files $uri $uri/ $uri.html /index.php?$args;
  }

  location ~ \.(css|html|htm|rtf|rtx|svg|svgz|txt|xsd|xsl|xml)$ {
      expires 3600s;
      add_header Pragma "public";
      add_header Cache-Control "max-age=3600, public";
      try_files $uri $uri/ $uri.html /index.php?$args;
  }

  location ~ \.(asf|asx|wax|wmv|wmx|avi|bmp|class|divx|doc|docx|eot|exe|gif|gz|gzip|ico|jpg|jpeg|jpe|json|mdb|mid|midi|mov|qt|mp3|m4a|mp4|m4v|mpeg|mpg|mpe|mpp|otf|_otf|odb|odc|odf|odg|odp|ods|odt|ogg|pdf|png|pot|pps|ppt|pptx|ra|ram|svg|svgz|swf|tar|tif|tiff|ttf|ttc|_ttf|wav|wma|wri|woff|woff2|xla|xls|xlsx|xlt|xlw|zip)$ {
      expires 31536000s;
      add_header Pragma "public";
      add_header Cache-Control "max-age=31536000, public";
      try_files $uri $uri/ $uri.html /index.php?$args;
  }

    location ~* \.(eot|ttf|woff)$ {
         add_header Access-Control-Allow-Origin *;
    }

    listen 443 ssl; # managed by Certbot
    listen [::]:443 ssl;
    ssl_certificate /etc/letsencrypt/live/www.example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/www.example.com/privkey.pem;
    include /etc/letsencrypt/options-ssl-nginx.conf;

    if ($scheme != "https") {
    return 301 https://$host$request_uri;
    }
}
  

以下答案均未解决我的问题。我目前在AWS上托管该网站,这可能是我设置我的存储桶重定向到域的方式吗?

0 个答案:

没有答案