Rewrite "https://files.mydomain.com/files" to "https://files.mydomain.com"

huangapple go评论80阅读模式
英文:

Rewrite "https://files.mydomain.com/files" to "https://files.mydomain.com"

问题

  1. location ^~ /files {
  2. rewrite ^/files/?(.*)$ https://files.mydomain.com/$1 permanent;
  3. proxy_pass http://localhost:89/files;
  4. }
英文:

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:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-properties -->

  1. http {
  2. server {
  3. listen 80;
  4. server_name localhost;
  5. return 301 https://$host$request_uri;
  6. }
  7. server {
  8. listen 80;
  9. server_name files.mydomain.com;
  10. location / {
  11. return 301 https://files.mydomain.com$request_uri;
  12. }
  13. }
  14. server {
  15. listen 443 ssl http2;
  16. listen [::]:443 ssl http2;
  17. server_name files.mydomain.com;
  18. client_max_body_size 0;
  19. location / {
  20. return 301 https://$host/files;
  21. }
  22. location /files {
  23. proxy_pass http://localhost:89;
  24. }
  25. }
  26. #Root Domain
  27. server {
  28. listen 443 ssl http2;
  29. listen [::]:443 ssl http2;
  30. server_name_in_redirect off;
  31. server_name www.mydomain.com mydomain.com;
  32. log_not_found off;
  33. location / {
  34. root /inetpub/wwwroot;
  35. index index.html index.htm;
  36. }
  37. }
  38. }

<!-- end snippet -->

My best attempt:
Unfortunately, it just takes me to the "Welcome to NGINX" webpage when I visit:
https://files.mydomain.com

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-properties -->

  1. http {
  2. server {
  3. listen 80;
  4. server_name localhost;
  5. return 301 https://$host$request_uri;
  6. }
  7. server {
  8. listen 80;
  9. server_name files.mydomain.com;
  10. location / {
  11. return 301 https://files.mydomain.com$request_uri;
  12. }
  13. }
  14. server {
  15. listen 443 ssl http2;
  16. listen [::]:443 ssl http2;
  17. server_name files.mydomain.com;
  18. client_max_body_size 0;
  19. location ^~ /files {
  20. rewrite ^/files/?(.*)$ https://files.mydomain.com/$1 permanent;
  21. proxy_pass http://localhost:89/files;
  22. }
  23. }
  24. #Root Domain
  25. server {
  26. listen 443 ssl http2;
  27. listen [::]:443 ssl http2;
  28. server_name_in_redirect off;
  29. server_name www.mydomain.com mydomain.com;
  30. log_not_found off;
  31. location / {
  32. root /inetpub/wwwroot;
  33. index index.html index.htm;
  34. }
  35. }
  36. }

<!-- end snippet -->

答案1

得分: 2

[Final answer]

在几次测试后,以下配置几乎满足了所有先决条件。

//files/ 都会被转发到 localhost:89/files/

  1. server {
  2. listen 443 ssl;
  3. ssl_certificate .../fullchain.pem;
  4. ssl_certificate_key .../privkey.pem;
  5. server_name files.mydomain.com;
  6. location /files/ {
  7. proxy_pass http://localhost:89/files/;
  8. }
  9. location / {
  10. proxy_pass http://localhost:89/files/;
  11. }
  12. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  13. proxy_set_header Host $host;
  14. proxy_http_version 1.1;
  15. proxy_set_header Upgrade $http_upgrade;
  16. proxy_set_header Connection "upgrade";
  17. }

在这个实际情况中,只有第一次访问时会遇到一个小问题:

> 当我第一次访问 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

[Initial answer]

我知道你要求进行URL重写,但你尝试过 proxy_pass + alias 方法吗?

在我家的测试中,看起来还可以。我只是将文件放在一个文件夹中,并使用以下nginx配置(我使用8443端口作为虚假的SSL端口):

  1. server {
  2. listen 8443;
  3. server_name files.ssl.localhost;
  4. root D:/WEB;
  5. location / {
  6. alias D:/WEB/secretfolder/files/;
  7. autoindex on; # 或者不用,只是用于测试文件夹列表
  8. }
  9. location /files/ { #注意末尾斜杠
  10. proxy_pass http://files.ssl.localhost:8443/; #注意末尾斜杠
  11. }
  12. }

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

Rewrite "https://files.mydomain.com/files" to "https://files.mydomain.com"

并且我们可以获取例如 file.txt 的内容:
Rewrite "https://files.mydomain.com/files" to "https://files.mydomain.com"

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

英文:

*** [Final answer] ***

After several tests, the following configuration covers almost all the prerequisites.

Both / and /files/ are forwarded to localhost:89/files/.

  1. server {
  2. listen 443 ssl;
  3. ssl_certificate .../fullchain.pem;
  4. ssl_certificate_key .../privkey.pem;
  5. server_name files.mydomain.com;
  6. location /files/ {
  7. proxy_pass http://localhost:89/files/;
  8. }
  9. location / {
  10. proxy_pass http://localhost:89/files/;
  11. }
  12. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  13. proxy_set_header Host $host;
  14. proxy_http_version 1.1;
  15. proxy_set_header Upgrade $http_upgrade;
  16. proxy_set_header Connection &quot;upgrade&quot;;
  17. }

In this real case, only a small issue at first visit :

> When I visit https://files.mydomain.com for the first time, it
> automatically forwards to:
> https://files.mydomain.com/files/login.aspx. After I authenticate with
> my credentials, it automatically forwards to
> https://files.mydomain.com/files instead of
> https://files.mydomain.com. However the good news is if I stay logged
> in and revisit https://files.mydomain.com, it will continue to stay
> at: https://files.mydomain.com.

[Initial answer]

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):

  1. server {
  2. listen 8443;
  3. server_name files.ssl.localhost;
  4. root D:/WEB;
  5. location / {
  6. alias D:/WEB/secretfolder/files/;
  7. autoindex on; # or not, only to test folder listing
  8. }
  9. location /files/ { #beware the trailing slash
  10. proxy_pass http://files.ssl.localhost:8443/; #beware the trailing slash
  11. }
  12. }

http://files.ssl.localhost:8443/files/ and http://files.ssl.localhost:8443 both point to the same directory listing:

Rewrite "https://files.mydomain.com/files" to "https://files.mydomain.com"

and we can get, for example, the contents of file.txt:
Rewrite "https://files.mydomain.com/files" to "https://files.mydomain.com"

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

答案2

得分: 0

尝试使用return而不是rewrite来进行重定向。应该会起作用:

  1. location /path/to-your-page-name {
  2. return 301 /new-path/to-your-new-page-name;
  3. }

所以,如果你想要将域名解析到~/files,并假设root指向/inetpub/wwwroot,请按照以下步骤操作:

  1. http {
  2. server {
  3. listen 80;
  4. server_name localhost;
  5. return 301 https://$host$request_uri;
  6. }
  7. server {
  8. listen 80;
  9. server_name files.mydomain.com;
  10. location / {
  11. return 301 https://files.mydomain.com$request_uri;
  12. }
  13. }
  14. server {
  15. listen 443 ssl http2;
  16. listen [::]:443 ssl http2;
  17. server_name files.mydomain.com;
  18. client_max_body_size 0;
  19. location / {
  20. return 301 https://files.mydomain.com/files;
  21. }
  22. }
  23. #根域名
  24. server {
  25. listen 443 ssl http2;
  26. listen [::]:443 ssl http2;
  27. server_name_in_redirect off;
  28. server_name www.mydomain.com mydomain.com;
  29. log_not_found off;
  30. location / {
  31. #root /inetpub/wwwroot;
  32. #index index.html index.htm;
  33. return 301 https://files.mydomain.com/files;
  34. }
  35. }
  36. }

根据你的配置,你可能还需要设置server_name_in_redirect on;。希望这能帮助你!

英文:

Try using a return and not a rewrite for a redirect. This should work:

  1. location /path/to-your-page-name {
  2. return 301 /new-path/to-your-new-page-name;
  3. }

So, if you want domains resolving to ~/files and assuming that root points to /inetpub/wwwroot do the following:

  1. http {
  2. server {
  3. listen 80;
  4. server_name localhost;
  5. return 301 https://$host$request_uri;
  6. }
  7. server {
  8. listen 80;
  9. server_name files.mydomain.com;
  10. location / {
  11. return 301 https://files.mydomain.com$request_uri;
  12. }
  13. }
  14. server {
  15. listen 443 ssl http2;
  16. listen [::]:443 ssl http2;
  17. server_name files.mydomain.com;
  18. client_max_body_size 0;
  19. location / {
  20. return 301 https://files.mydomain.com/files;
  21. }
  22. }
  23. #Root Domain
  24. server {
  25. listen 443 ssl http2;
  26. listen [::]:443 ssl http2;
  27. server_name_in_redirect off;
  28. server_name www.mydomain.com mydomain.com;
  29. log_not_found off;
  30. location / {
  31. #root /inetpub/wwwroot;
  32. #index index.html index.htm;
  33. return 301 https://files.mydomain.com/files;
  34. }
  35. }
  36. }

You might also have to set server_name_in_redirect on; depending on your configuration.

I hope this helps!

huangapple
  • 本文由 发表于 2023年5月11日 06:12:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/76222898.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定