英文:
Rewrite "https://files.mydomain.com/files" to "https://files.mydomain.com"
问题
location ^~ /files {
rewrite ^/files/?(.*)$ https://files.mydomain.com/$1 permanent;
proxy_pass http://localhost:89/files;
}
英文:
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 -->
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;
}
}
}
<!-- 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 -->
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;
}
}
}
<!-- end snippet -->
答案1
得分: 2
[Final answer]
在几次测试后,以下配置几乎满足了所有先决条件。
/
和 /files/
都会被转发到 localhost:89/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/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";
}
在这个实际情况中,只有第一次访问时会遇到一个小问题:
> 当我第一次访问 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端口):
server {
listen 8443;
server_name files.ssl.localhost;
root D:/WEB;
location / {
alias D:/WEB/secretfolder/files/;
autoindex on; # 或者不用,只是用于测试文件夹列表
}
location /files/ { #注意末尾斜杠
proxy_pass http://files.ssl.localhost:8443/; #注意末尾斜杠
}
}
http://files.ssl.localhost:8443/files/
和 http://files.ssl.localhost:8443
都指向相同的目录列表:
我希望这种方法符合你的目标,或者对其他人有意义。
英文:
*** [Final answer] ***
After several tests, the following configuration covers almost all the prerequisites.
Both /
and /files/
are forwarded to localhost:89/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/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";
}
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):
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:
and we can get, for example, the contents of file.txt:
I hope this approach matches your goals or makes sense for anyone else.
答案2
得分: 0
尝试使用return
而不是rewrite
来进行重定向。应该会起作用:
location /path/to-your-page-name {
return 301 /new-path/to-your-new-page-name;
}
所以,如果你想要将域名解析到~/files,并假设root
指向/inetpub/wwwroot
,请按照以下步骤操作:
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://files.mydomain.com/files;
}
}
#根域名
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;
return 301 https://files.mydomain.com/files;
}
}
}
根据你的配置,你可能还需要设置server_name_in_redirect on;
。希望这能帮助你!
英文:
Try using a return
and not a rewrite
for a redirect. This should work:
location /path/to-your-page-name {
return 301 /new-path/to-your-new-page-name;
}
So, if you want domains resolving to ~/files and assuming that root
points to /inetpub/wwwroot
do the following:
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://files.mydomain.com/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;
return 301 https://files.mydomain.com/files;
}
}
}
You might also have to set server_name_in_redirect on;
depending on your configuration.
I hope this helps!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论