nginx 本地化重定向不起作用

83qze16e  于 2023-10-17  发布在  Nginx
关注(0)|答案(1)|浏览(231)

我有一个Facebook docusaurus和docker的网站。下面是docker-compose.yml

version: "3"
services:
  docusaurus:
    restart: unless-stopped
    image: staticfloat/nginx-certbot
    ports:
      - 80:80/tcp
      - 443:443/tcp
    environment:
      CERTBOT_EMAIL: [email protected]
    volumes:
      - ./conf.d:/etc/nginx/user.conf.d:ro
      - ./build:/app
      - letsencrypt:/etc/letsencrypt

volumes:
  letsencrypt:

这里是conf.d/server-block.conf

gzip on;
gzip_proxied any;
gzip_disable "msie6";
gzip_vary on;
gzip_proxied any;
gzip_comp_level 6;
gzip_buffers 16 8k;
gzip_http_version 1.1;
gzip_min_length 256;
gzip_types text/plain text/css application/json application/javascript application/x-javascript text/xml application/xml application/rss+xml text/javascript application/vnd.ms-fontobject application/x-font-ttf font/opentype image/jpeg image/png image/svg+xml image/x-icon;

server {
  listen  3002;
  absolute_redirect off;
  root  /app;

  location = / {
    rewrite ^(.*)$ https://$http_host/docs/introduction redirect;
  }

  location = /docs {
    rewrite ^(.*)$ https://$http_host/docs/introduction redirect;
  }

  location = /addinHelp {
    rewrite ^(.*)$ https://$http_host/docs/introduction redirect;
  }

  location = /privacy {
    rewrite ^(.*)$ https://$http_host/privacy.html redirect;
  }

  location = /docs/helpdeskAndContact {
    rewrite ^(.*)$ https://$http_host/docs/helpdesk-contact redirect;
  }

  location / {
    try_files $uri $uri/ =404;
  }
}

upstream backend {
   server 178.62.87.72:443;
}

server {
    listen              443 ssl;
    ssl_certificate     /etc/letsencrypt/live/www.example.tech/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/www.example.tech/privkey.pem;
    server_name www.example.tech;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_prefer_server_ciphers on;
    ssl_session_timeout 1d;
    ssl_stapling on;
    ssl_stapling_verify on;
    add_header Strict-Transport-Security max-age=15768000;
    add_header X-Frame-Options "";

    proxy_ssl_name "www.backend.io";
    proxy_ssl_server_name on;
   
    location ~ /auth/(.*) {                                                                                            
          proxy_pass  https://backend/example/auth/$1?$query_string;
          proxy_set_header Host www.example.tech;
    }

    location / {
        proxy_set_header    Host                $host;
        proxy_set_header    X-Forwarded-For     $proxy_add_x_forwarded_for;
        proxy_set_header    X-Forwarded-Proto   $scheme;
        proxy_set_header    Accept-Encoding     "";
        proxy_set_header    Proxy               "";
        proxy_pass          http://docusaurus:3002/;

        # These three lines added as per https://github.com/socketio/socket.io/issues/1942 to remove socketio error
        proxy_http_version 1.1;
        proxy_set_header   Upgrade $http_upgrade;
        proxy_set_header   Connection "upgrade";
    }
}

现在,问题是我所有的中文网页都以www.example.tech/zh-CN/开头,例如www.example.tech/zh-CN/docs/introduction。我想让zh-CN不区分大小写,这样www.example.tech/zh-cn/...www.example.tech/ZH-cn/...也可以工作。
ChatGPT建议我将以下重定向添加到443服务器块:

location ~* ^/zh-(cn|CN)(/.*)?$ {
        rewrite ^/zh-(cn|CN)(/.*)?$ /zh-CN$2 last;
    }

我试过了,但是浏览器说有很多重定向。它只是没有工作。
有人能帮忙吗?

mitkmikd

mitkmikd1#

我试过了,但是浏览器说有很多重定向。
可能是因为rewrite即使在URL被重写为所需格式后也会触发,导致Nginx不断尝试重定向已经正确的URL。
对于一个全面的解决方案,你可以使用一个通用的catch,用于“zh“的任何大小写组合,后跟“cn“的任何大小写组合,并确保它不匹配已经正确的格式。

location ~* ^/[Zz][Hh]-(?!CN)[Cc][Nn](/.*)?$ {
    rewrite ^/[Zz][Hh]-[Cc][Nn](/.*)?$ /zh-CN$1 last;
}

有:

  • ^/[Zz][Hh]-(?!CN)[Cc][Nn](/.*)?$:匹配不同情况下/zh-CN的任何变化,但由于negative lookahead(?!CN),它不匹配确切的/zh-CN
  • [Zz][Hh]-[Cc][Nn]将捕获/zh-CN在不同情况下的所有变体,并将其重写为正确的格式。

相关问题