Nginx 将 *.domain.tld/page 重写为 domain.tld/page

Nginx 将 *.domain.tld/page 重写为 domain.tld/page

我无法设置从 *.domain.tld/personal 到 domain.tld/personal 的重定向

server {
    server_name domain.tld www.domain.tld *.domain.tld;
    listen 100.10.10.1;
    set $root_path /var/www/user1536/data/www/new.domain.tld;
    location ~* ^.+\.(jpg|jpeg|gif|png|svg|js|css|mp3|ogg|mpe?g|avi|zip|gz|bz2?|rar|swf)$ {
        root $root_path;
        access_log /var/www/nginx-logs/user1536 isp;
        access_log /var/www/httpd-logs/domain.tld.access.log ;
        error_page 404 = @fallback;
    }

    location /personal {
        if ($http_host != domain.tld) { rewrite ^ http://domain.tld/personal; }
    }

    location / {
        proxy_pass http://100.10.10.1:81;
        proxy_redirect http://100.10.10.1:81/ /;
        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 X-Real-IP $remote_addr;
    }
    location ~* ^/(webstat|awstats|webmail|myadmin|pgadmin)/ {
        proxy_pass http://100.10.10.1:81;
        proxy_redirect http://100.10.10.1:81/ /;
        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 X-Real-IP $remote_addr;
    }
    location @fallback {
        proxy_pass http://100.10.10.1:81;
        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 X-Real-IP $remote_addr;
    }
    location ^~ /webstat/ {
        auth_basic "Restricted area";
        auth_basic_user_file /var/www/user1536/data/etc/42338471.passwd;
        try_files $uri @fallback;
    }
    include /usr/local/ispmgr/etc/nginx.inc;
}

答案1

您应该使用另一个服务器块进行重定向。

server {
    server_name *.domain.tld;
    listen 100.10.10.1;

    location /personal {
        rewrite ^ http://domain.tld/personal permanent;
    }
}

然后,您应该location从另一个server块中删除该块,并*.domain.tldserver_name指令中删除。

相关内容