我在我的1纯HTML文件网站上运行Pingdomtesting,我得到这样的评分:
如果可能,请删除以下redirect链:
http://example.com/ http://www.example.com/ https://www.example.com/
而我的.conf中唯一的redirect是:
server { listen 80; server_name example.com; return 301 $scheme://www.example.com$request_uri; }
下一个块是SSL和其余的。
server { listen 443 ssl http2; etc... }
没有其他configuration。 我所需要的是将非wwwredirect到www,并且始终仅https并通过testing。
更新:完整.conf
server { listen 80; server_name example.com; return 301 https://www.example.com$request_uri; } server { listen 443 ssl http2; ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; ssl_protocols TLSv1 TLSv1.1 TLSv1.2; ssl_prefer_server_ciphers on; ssl_ciphers 'EECDH+CHACHA20:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256:EECDH+3DES:RSA+3DES:!MD5'; ssl_dhparam /etc/nginx/ssl/dhparams.pem; ssl_session_timeout 1d; ssl_session_cache shared:SSL:50m; ssl_stapling on; ssl_stapling_verify on; add_header Strict-Transport-Security max-age=15768000; root /var/www/example.com/htdocs; server_name example.com www.example.com; location / { autoindex on; try_files $uri $uri/ =404; } location ~* /img/.*\.gif$ { expires 30d; add_header Pragma public; add_header Cache-Control "public"; } }
只要将$scheme改为https ,我就不能说出为什么那里是第一位的,因为除了你的用户做了一些奇怪的事情之外,你不可能得到除了端口80之外的其他东西。
编辑:添加缺less的https-> httpsredirect。
没有注意到你实际上没有明确的default_server 。 沿着这样的方向走:
server { listen 80; server_name example.com www.example.com; return 301 https://www.example.com$request_uri; } server { listen 443; server_name example.com; return 301 https://www.example.com$request_uri; }
阿希什的答案可能会起作用,但if可能的if ,我会远离(阅读“ 如果是邪恶” )。
我想你的godaddy cname或者Alogging对于nginx会好的,试试下面的代码。
server { listen 80 default_server; listen [::]:80 default_server; if ($http_x_forwarded_proto != 'https') { rewrite ^(.*) https://www.example.com$1 redirect; } }
如果你想一口气做到这一点,那么解决这个问题是非常糟糕的。 在服务器80块上的configuration和位置块顶部添加地图模块。
map $http_host $new { 'www.abc.com' '1'; } server { listen 80 default_server; listen [::]:80 default_server; if ($http_x_forwarded_proto != 'https') { rewrite ^(.*) https://$host$1 redirect; } location / { if ($new != '1') { rewrite ^(.*) https://www.example.com$1 redirect; } } }