如何使用 nginx 在浏览器中重写 url 而不更改 url

how can I rewrite url without changing url in the browser with nginx

我的 wordpress 中有一个静态页面,

https://www.mrtrobotics.com/naplan

并且我想为此页面使用子域

edu.mrtrobotics.com

现在当我使用 edu.mrtrobotics.com ---> 它转到 https://www.mrtrobotics.com/naplan

但是在浏览器url中显示为https://www.mrtrobotics.com/naplan

我希望它向用户显示为 edu.mrtrobotics.com。

我该怎么办??

server {
    listen 80;
    listen [::]:80;

    server_name mrtrobotics.com www.mrtrobotics.com;

    location ~ /.well-known/acme-challenge {
        allow all;
        root /var/www/html;
    }

    location / {
        rewrite ^ https://$host$request_uri? permanent;
    }
}

server {
    listen 80;
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    server_name edu.mrtrobotics.com;

    ssl_certificate /etc/letsencrypt/live/mrtrobotics.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/mrtrobotics.com/privkey.pem;

    include /etc/nginx/conf.d/options-ssl-nginx.conf;

    rewrite ^ https://mrtrobotics.com/naplan permanent;
}

server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    server_name mrtrobotics.com www.mrtrobotics.com;

    index index.php index.html index.htm;

    root /var/www/html;

    server_tokens off;

    ssl_certificate /etc/letsencrypt/live/mrtrobotics.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/mrtrobotics.com/privkey.pem;

    include /etc/nginx/conf.d/options-ssl-nginx.conf;

    add_header X-Frame-Options "SAMEORIGIN" always;
    add_header X-XSS-Protection "1; mode=block" always;
    add_header X-Content-Type-Options "nosniff" always;
    add_header Referrer-Policy "no-referrer-when-downgrade" always;
    add_header Content-Security-Policy "default-src * data: 'unsafe-eval' 'unsafe-inline'" always;
    # add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload" always;
    # enable strict transport security only if you understand the implications

    location / {
        try_files $uri $uri/ /index.php$is_args$args;
    }

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass wordpress:9000;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }

    location ~ /\.ht {
        deny all;
    }

    location = /favicon.ico {
        log_not_found off; access_log off;
    }
    location = /robots.txt {
        log_not_found off; access_log off; allow all;
    }
    location ~* \.(css|gif|ico|jpeg|jpg|js|png)$ {
        expires max;
        log_not_found off;
    }
}

# Set client upload size - 100Mbyte
client_max_body_size 100M;

# to avoid 504 time out error - defalut is 60s
proxy_send_timeout 180s;
proxy_read_timeout 180s;
fastcgi_send_timeout 180s;
fastcgi_read_timeout 180s;

您处理 edu.mrtrobotics.com 的服务器不应该有该重写规则,一旦您进行了永久重写,您将向浏览器发送 301 并且 URL 将会改变。如果 /naplan 真的是唯一应该从 edu 子域提供的东西,那么也许你想要这样的东西?

server {
    listen 80;
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    server_name edu.mrtrobotics.com;

    ssl_certificate /etc/letsencrypt/live/mrtrobotics.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/mrtrobotics.com/privkey.pem;

    include /etc/nginx/conf.d/options-ssl-nginx.conf;
    root /var/www/html;

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

更新,因为 /naplan 是一个文件夹,试试...

server {
    listen 80;
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    server_name edu.mrtrobotics.com;

    ssl_certificate /etc/letsencrypt/live/mrtrobotics.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/mrtrobotics.com/privkey.pem;

    include /etc/nginx/conf.d/options-ssl-nginx.conf;
    root /var/www/html;

    location / {
        root /var/www/html/naplan;
    }
}

祝你好运:-)