看了 nginx 的 location 优先匹配不是先精准再普通最后正则么, 不知道为什么我的配置却总是直接下面的返匹配跳转 301, 换成 [ location ~ /.+ ] 也没用。。。 求大佬解答下,这是为什么~~~
server { server_name www.a.com a.com; listen 80 ; access_log /var/log/nginx/access.log; location /.well-known/acme-challenge/ { auth_basic off; allow all; root /usr/share/nginx/html; try_files $uri =404; break; }
rewrite ^(.*) https://$server_name$1 permanent; }
server { server_name www.a.com a.com; listen 443 ssl http2 ; access_log /var/log/nginx/access.log; ssl_session_timeout 5m; ssl_session_cache shared:SSL:50m; ssl_session_tickets off; ssl_certificate /etc/nginx/certs/www.a.com.crt; ssl_certificate_key /etc/nginx/certs/www.a.com.key; add_header Strict-Transport-Security "max-age=31536000" always;
root /home;
location =/ { try_files '' /index.html; }
location / { return 301 https://2.a.com$request_uri; } }
1
msg7086 2021-01-13 15:41:52 +08:00
try_files 跳转到 /index.html 后会触发 301 。
你需要加一个 location = /index.html {} 来避免被 location / 带走。 |