V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
NGINX
NGINX Trac
3rd Party Modules
Security Advisories
CHANGES
OpenResty
ngx_lua
Tengine
在线学习资源
NGINX 开发从入门到精通
NGINX Modules
ngx_echo
dcd
V2EX  ›  NGINX

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

  •  
  •   dcd · 2019-09-07 12:50:26 +08:00 · 3091 次点击
    这是一个创建于 1889 天前的主题,其中的信息可能已经有所发展或是发生改变。

    设想

    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;
    	}
    }
    
    3 条回复    2019-09-07 13:16:53 +08:00
    KasuganoSoras
        1
    KasuganoSoras  
       2019-09-07 13:07:34 +08:00   ❤️ 1
    因为你开了 HSTS,开启之后它会先将本域名( a.com )跳转到 https 协议,然后才会去访问服务器,接着匹配到了跳转到 www.a.com 才进行跳转。
    KasuganoSoras
        2
    KasuganoSoras  
       2019-09-07 13:09:12 +08:00
    而且你的 HSTS 是 preload,也就是在访问之前就会预先跳转,然后就——自动跳转了。
    dcd
        3
    dcd  
    OP
       2019-09-07 13:16:53 +08:00
    @KasuganoSoras 谢谢大佬!
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   4944 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 09:43 · PVG 17:43 · LAX 01:43 · JFK 04:43
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.