将“https://files.mydomain.com/files”重写为“https://files.mydomain.com”

MKA*_*NET 5 nginx nginx-reverse-proxy nginx-config

目前,我可以访问: https://files.mydomain.com/files。即使我去https://files.mydomain.com,它也会自动重定向到https://files.mydomain.com/files成功。

相反,我希望 NGINX 自动重写https://files.mydomain.com/files->https://files.mydomain.com

我当前的 NGINX 代码:

http {

    server {
        
            listen       80;
            server_name  localhost;
            return 301 https://$host$request_uri;
    }

    server {

            listen       80;
            server_name  files.mydomain.com;
            location / {
                return 301 https://files.mydomain.com$request_uri;
        }
    }

    server {
            listen 443 ssl http2; 
            listen [::]:443 ssl http2;
            server_name  files.mydomain.com;
            client_max_body_size 0;
            location / {
                return 301 https://$host/files;
            } 
            location /files {
                proxy_pass http://localhost:89;
            }
    }

    #Root Domain
    server {
            listen 443 ssl http2; 
            listen [::]:443 ssl http2;
            server_name_in_redirect off;
            server_name  www.mydomain.com mydomain.com;
            log_not_found off;

        location / {
            root   /inetpub/wwwroot;
            index  index.html index.htm;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我的最佳尝试:
不幸的是,当我访问时,它只是带我进入“欢迎来到 NGINX”网页: https://files.mydomain.com

http {

    server {
        
            listen       80;
            server_name  localhost;
            return 301 https://$host$request_uri;
    }

    server {

            listen       80;
            server_name  files.mydomain.com;
            location / {
                return 301 https://files.mydomain.com$request_uri;
        }
    }

    server {
            listen 443 ssl http2; 
            listen [::]:443 ssl http2;
            server_name  files.mydomain.com;
            client_max_body_size 0;
            location ^~ /files {
                rewrite ^/files/?(.*)$ https://files.mydomain.com/$1 permanent;
                proxy_pass http://localhost:89/files;
            }
    }

    #Root Domain
    server {
            listen 443 ssl http2; 
            listen [::]:443 ssl http2;
            server_name_in_redirect off;
            server_name  www.mydomain.com mydomain.com;
            log_not_found off;

        location / {
            root   /inetpub/wwwroot;
            index  index.html index.htm;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Phi*_*ppe 2

*** [最终答案] ***

经过多次测试,以下配置几乎涵盖了所有前提条件。

和 均/转发/files/localhost:89/files/

server {
    listen 443 ssl;
    ssl_certificate .../fullchain.pem;
    ssl_certificate_key .../privkey.pem;

    server_name files.mydomain.com;

    location /files/ { 
        proxy_pass http://localhost:89/files/;
    }       

    location / { 
        proxy_pass http://localhost:89/files/;
    }
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $host;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
}
Run Code Online (Sandbox Code Playgroud)

在这个真实的案例中,第一次访问时只有一个小问题:

当我第一次 访问https://files.mydomain.com时,它会自动转发到: https://files.mydomain.com/files/login.aspx。在我使用凭据进行身份验证后,它会自动转发到 https://files.mydomain.com/files而不是 https://files.mydomain.com。然而,好消息是,如果我保持登录状态并重新访问https://files.mydomain.com,它将继续停留在:https://files.mydomain.com

[初步回答]

我知道你要求重写 url,但是你尝试过proxy_pass+alias方式吗?

我在家测试好像没问题。我只是将文件放在一个文件夹中并使用以下 nginx 配置(我使用端口 8443 作为假 SSL 端口):

server {
    listen 8443;
    server_name files.ssl.localhost;
    root D:/WEB;

    location / {
        alias D:/WEB/secretfolder/files/;
        autoindex on; # or not, only to test folder listing
    }

    location /files/ { #beware the trailing slash
        proxy_pass http://files.ssl.localhost:8443/; #beware the trailing slash
    }
}
Run Code Online (Sandbox Code Playgroud)

http://files.ssl.localhost:8443/files/并且http://files.ssl.localhost:8443都指向相同的目录列表:

目录列表

例如,我们可以获取file.txt的内容: 文件.txt内容

我希望这种方法符合您的目标或对其他人有意义。