Nginx-301将www和非www重定向到https-non-www不起作用 [英] Nginx - 301 redirect www and non-www to https-non-www not working

查看:129
本文介绍了Nginx-301将www和非www重定向到https-non-www不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从www和非www重定向到我网站的https-non-www uri无效.

The redirection I made from www and non-www to the https-non-www uri of my web site doesn't work.

这是我的服务器块:

server {
       listen           80;
       listen           [::]:80;
       server_name      gfelot.xyz www.gfelot.xyz;
       return           301 https://gfelot.xyz$request_uri;
}

server {
       listen           443 ssl http2 default_server;
       listen           [::]:443 ssl http2 default_server;
       include          snippets/ssl-gfelot.xyz.conf;
       include          snippets/ssl-params.conf;

       server_name      gfelot.xyz;

       access_log       /var/log/nginx/gfelot.xyz.access.log;
       error_log        /var/log/nginx/gfelot.xyz.log;

       root /var/www/html;
       index index.html index.htm

       error_page       404 /404.html;
       error_page       500 502 503 504 /50x.html;

       location = /50x.html {
                root /usr/share/nginx/html;
       }

       location ~* \.css$ {
                 access_log off;
                 expires 1M;
                 add_header Pragma public;
                 add_header Cache-Control public;
                 add_header Vary Accept-Encoding;
       }
}

包括ssl-gfelot.xyz.conf:

include ssl-gfelot.xyz.conf :

ssl_certificate /etc/letsencrypt/live/gfelot.xyz/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/gfelot.xyz/privkey.pem;

包括ssl-params.conf:

include ssl-params.conf :

ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
ssl_ciphers "EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH";
ssl_ecdh_curve secp384r1;
ssl_session_cache shared:SSL:10m;
ssl_session_tickets off;
ssl_stapling on;
ssl_stapling_verify on;
resolver 8.8.8.8 8.8.4.4 valid=300s;
resolver_timeout 5s;
# Disable preloading HSTS for now.  You can use the commented out header line that includes
# the "preload" directive if you understand the implications.
#add_header Strict-Transport-Security "max-age=63072000; includeSubdomains; preload";
add_header Strict-Transport-Security "max-age=63072000; includeSubdomains";
add_header X-Frame-Options SAMEORIGIN;
add_header X-Content-Type-Options nosniff;

ssl_dhparam /etc/ssl/certs/dhparam.pem;

推荐答案

server {
   listen           80;
   listen           [::]:80;
   server_name      gfelot.xyz www.gfelot.xyz;
   return           301 https://gfelot.xyz$request_uri;
}

server {
   listen           443;
   server_name      gfelot.xyz;

   ssl on;
   ssl_certificate  < your ssl file ssl-xyx.crt path>;
   ssl_certificate_key <ssl keyfile.key path>;
   access_log       /var/log/nginx/gfelot.xyz.access.log;
   error_log        /var/log/nginx/gfelot.xyz.log;

   root /var/www/html;
   index index.html index.htm

   error_page       404 /404.html;
   error_page       500 502 503 504 /50x.html;

   location = /50x.html {
            root /usr/share/nginx/html;
   }

   location ~* \.css$ {
             access_log off;
             expires 1M;
             add_header Pragma public;
             add_header Cache-Control public;
             add_header Vary Accept-Encoding;
   }
}

这篇关于Nginx-301将www和非www重定向到https-non-www不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆