NGINX
NGINX Trac
3rd Party Modules
Security Advisories
CHANGES
OpenResty
ngx_lua
Tengine
在线学习资源
NGINX 开发从入门到精通
NGINX Modules
ngx_echo
dcd
V2EX  ›  NGINX

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

  •  
  •   dcd · Sep 7, 2019 · 3967 views
    This topic created in 2445 days ago, the information mentioned may be changed or developed.

    设想

    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 replies    2019-09-07 13:16:53 +08:00
    KasuganoSoras
        1
    KasuganoSoras  
       Sep 7, 2019   ❤️ 1
    因为你开了 HSTS,开启之后它会先将本域名( a.com )跳转到 https 协议,然后才会去访问服务器,接着匹配到了跳转到 www.a.com 才进行跳转。
    KasuganoSoras
        2
    KasuganoSoras  
       Sep 7, 2019
    而且你的 HSTS 是 preload,也就是在访问之前就会预先跳转,然后就——自动跳转了。
    dcd
        3
    dcd  
    OP
       Sep 7, 2019
    @KasuganoSoras 谢谢大佬!
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1187 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 32ms · UTC 17:52 · PVG 01:52 · LAX 10:52 · JFK 13:52
    ♥ Do have faith in what you're doing.