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
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):
http://files.ssl.localhost:8443/files/
andhttp://files.ssl.localhost:8443
both point to the same directory listing:and we can get, for example, the contents of file.txt:
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 :
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?
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)[
UPDATE 4
]Indeed, in your case, it seems we must do some url rewriting. Would you test this new proposition?
[
UPDATE 5
]IMHO, elements of my initial answer where also useful, here is another proposition :
This is a theoretical test, please let me know its behaviour with your server.
Try using a
return
a not arewrite
for a redirect. This should work:I hope this helps!