Nginx 问题 - URL 后的特殊字符

Nginx 问题 - URL 后的特殊字符

我的网站有尾随 #! 类似www.mywebsite.com/#

我想将其重定向到 404。我的 conf.d/mywebsite.com.conf 中有服务器块和位置块

server {
    server_name    mywebsite.com www.mywebsite.com;
    root           /var/www/mywebsite/public;
    index          index.php index.html;

    if ($host = mywebsite.com) {
        return 301 https://www.mywebsite.com$request_uri;
    }
    location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                #try_files $uri $uri/ =404;
                try_files $uri $uri/ /index.php?$query_string;
        }
        location /blog {
                try_files $uri $uri/ /blog/index.php?$args;
        }
        location ~ \.php$ {
                include snippets/fastcgi-php.conf;
                fastcgi_pass unix:/run/php/php7.3-fpm.sock;
        }
    gzip             on;
    gzip_comp_level  3;
    gzip_types       text/plain text/css application/javascript image/*;

    client_max_body_size 10m;

}
server {
    if ($host = www.mywebsite.com) {
        return 301 https://www.mywebsite.com$request_uri;
    } # managed by Certbot


    if ($host = mywebsite.com) {
        return 301 https://www.mywebsite.com$request_uri;
    } # managed by Certbot


    listen         80;
    listen         [::]:80;
    server_name    mywebsite.com www.mywebsite.com;
    return 404; # managed by Certbot
}

答案1

哈希 (#) 之后永远不会发送给服务器。因此,编写一条以哈希 (#) 开头的 URL 部分永远不会发送给服务器的规则是没有意义的。

假设您有以下 URL:

site.com/my_data_here
site.com/my_data_here#xyz

你可以为该my_data_here部分写一些条件。浏览器不会将xyz参数发送到服务器。

相关内容