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

问题描述 投票:0回答:1

目前,我可以访问:

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;
        }
    }
}

我最好的尝试:
不幸的是,当我访问时,它只是将我带到“欢迎使用 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;
        }
    }
}
nginx nginx-reverse-proxy nginx-config
1个回答
0
投票

我知道您要求重写 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
    }
}

http://files.ssl.localhost:8443/files/
http://files.ssl.localhost:8443
都指向同一个目录列表:

我们可以得到例如file.txt的内容:

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

© www.soinside.com 2019 - 2024. All rights reserved.