skip to Main Content

Currently, I am able to visit: https://files.mydomain.com/files. Even if I go to https://files.mydomain.com, it will redirect automatically to https://files.mydomain.com/files successfully.

Instead, I would like for NGINX to automatically rewrite https://files.mydomain.com/files -> https://files.mydomain.com

My current NGINX code:

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

My best attempt:
Unfortunately, it just takes me to the "Welcome to NGINX" webpage when I visit:
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;
        }
    }
}

2

Answers


  1. I know you are asking for url rewriting, but have you tried the proxy_pass+alias way?

    My tests at home seem ok. I simply put files in a folder and use the following nginx configuration (I use port 8443 as a fake SSL port):

    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/ and http://files.ssl.localhost:8443 both point to the same directory listing:

    directory listing

    and we can get, for example, the contents of file.txt:
    file.txt contents

    I hope this approach matches your goals or makes sense for anyone else.

    [UPDATE 1]

    In order to better match your configuration, maybe this could be more clear :

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

    Here too, http://files.ssl.localhost:443 & http://files.ssl.localhost:443/files point to the same folder, served on port 89.

    [UPDATE 2]

    As we don’t have to define port 89, served by IIS, would you try such a piece of code?

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

    It works with my SSL domain name, I don’t have any IIS server but it could match.

    [UPDATE 3]

    So, as your IIS server seems to expose its ../files folder on route /, would you try the following suggestion? (a mix of 2 previous ones)

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

    [UPDATE 4]

    Indeed, in your case, it seems we must do some url rewriting. Would you test this new proposition?

    server {
        listen 443 ssl;
        ssl_certificate .../fullchain.pem;
        ssl_certificate_key .../privkey.pem;
    
        server_name files.mydomain.com;     
    
        location ~ /files(.*)$ { 
            return 302 https://files.mydomain.com$1; # 302 for tests purpose
        }
    
        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";
        }
    }
    

    [UPDATE 5]

    IMHO, elements of my initial answer where also useful, here is another proposition :

        ......
        
        location / { 
            autoindex on; # for tests purpose
            alias D:/WEB/secretfolder/files/; 
            ## replace with the REAL path to the files folder on your server
        }
    
        location /files {
            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";    
        }
        ......
    

    This is a theoretical test, please let me know its behaviour with your server.

    Login or Signup to reply.
  2. Try using a return a not a rewrite for a redirect. This should work:

    location /path/to-your-page-name {
      return 301 /new-path/to-your-new-page-name;
    }
    

    I hope this helps!

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search