烦请各位大佬帮忙看一下这个 nginx 配置 301 跳转哪里写错了

2019-09-07 12:50:26 +08:00
 dcd

设想

http://a.com
http://www.a.com
http://b.com
http://www.b.com

全部 301 跳转至https://www.a.com

但是现在存在的问题是http://a.com会先跳转到https://a.com再跳转到https://www.a.com

本人是小白菜鸟,烦请各位大佬帮忙看看配置文件需要怎么修改,拜谢!

下面是 nginx 配置文件

server {
    listen 443 ssl http2 reuseport;
    server_name                 a.com www.a.com m.a.com b.com www.b.com m.b.com;
    index                       index.html index.htm index.php;
    root                        /home/www;
    ssl on;
    ssl_certificate             /home/ssl/fullchain.pem;
    ssl_certificate_key         /home/ssl/a.com.pem;
    ssl_dhparam                 /home/ssl/dhparam.pem;
    ssl_session_timeout         1d;
    ssl_protocols               TLSv1.2 TLSv1.3;
    ssl_prefer_server_ciphers   on;
    ssl_ciphers                 TLS13-AES-256-GCM-SHA384:TLS13-CHACHA20-POLY1305-SHA256:TLS13-AES-128-GCM-SHA256:TLS13-AES-128-CCM-8-SHA256:TLS13-AES-128-CCM-SHA256:EECDH+CHACHA20:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256:EECDH+3DES:RSA+3DES:!MD5;
    ssl_session_cache shared:SSL:50m;
    ssl_session_tickets         on;
    ssl_stapling                on;
    ssl_stapling_verify         on;
    resolver                    114.114.114.114 valid=300s;
    resolver_timeout            10s;
    if ($host = 'a.com') {
        return 301 https://www.a.com$request_uri;
    }
    if ($host = 'b.com') {
        return 301 https://www.a.com$request_uri;
    }
    if ($host = "www.b.com") {
        return 301 https://www.a.com$request_uri;
    }
    if (!-e $request_filename) {
        rewrite ^/(.*)  /index.php?s=$1 last;
    }
    location ~ [^/]\.php(/|$) {
        fastcgi_pass             unix:/dev/shm/php-fpm.sock;
        fastcgi_index            index.php;
        include                  fastcgi.conf;
        fastcgi_param            HTTPS on;
        add_header               Strict-Transport-Security "max-age=31536000; includeSubDomains; preload";
        add_header               X-Content-Type-Options nosniff;
    }
    error_page 404 500 /404.html;
    access_log  /home/logs/nginx_access.log;
    error_log   /home/logs/nginx_error.log;
}
server {
	listen 80;
    server_name a.com www.a.com b.com www.b.com;
    access_log off;
    error_log off;
	location / {
		return 301 https://www.a.com$request_uri;
	}
}
server {
	listen 80;
    server_name m.a.com m.b.com;
    access_log off;
    error_log off;location / {
		return 301 https://m.a.com$request_uri;
	}
}
3138 次点击
所在节点    NGINX
3 条回复
KasuganoSoras
2019-09-07 13:07:34 +08:00
因为你开了 HSTS,开启之后它会先将本域名( a.com )跳转到 https 协议,然后才会去访问服务器,接着匹配到了跳转到 www.a.com 才进行跳转。
KasuganoSoras
2019-09-07 13:09:12 +08:00
而且你的 HSTS 是 preload,也就是在访问之前就会预先跳转,然后就——自动跳转了。
dcd
2019-09-07 13:16:53 +08:00
@KasuganoSoras 谢谢大佬!

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/598822

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX