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

9jyewag0  于 2023-05-22  发布在  Nginx
关注(0)|答案(1)|浏览(187)

bounty还有2天到期。回答此问题可获得+350声望奖励。MKANET正在寻找典型答案

目前,我可以访问: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;
        }
    }
}
qlfbtfca

qlfbtfca1#

我知道你在要求重写网址,但你试过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的内容:

我希望这种方法符合你的目标,或者对其他任何人都有意义。
[UPDATE 1]
为了更好地匹配您的配置,也许这可以更清楚:

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

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

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

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

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

这里,http://files.ssl.localhost:443http://files.ssl.localhost:443/files也指向同一个文件夹,服务于端口89。
[UPDATE 2]
由于我们不需要定义由IIS提供服务的端口89,您会尝试这样的代码吗?

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

它与我的SSL域名,我没有任何IIS服务器,但它可以匹配。
[UPDATE 3]
因此,由于您的IIS服务器似乎在路由/上暴露了其../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/;
    }       

    location / { 
        proxy_pass http://localhost:89/;
    }
    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";
}

相关问题