简体   繁体   English

Nginx重定向到错误的URL

[英]Nginx is redirecting to the wrong url

I have been trying to figure this out for almost 6 months on and off I am at a loss. 我一直想弄清楚这个问题已经有将近6个月了,而我却一头雾水。 I have read every article I could on stack overflow even remotely related to my issue and nothing works. 我读过我可能会在堆栈上溢出的每篇文章,甚至与我的问题有关,而且什么也没有。 I have one machine running redmine and bookstack. 我有一台机器运行redmine和书架。 I have nginx on that machine and configured to use redmine.home.mydomain.com and kb.home.mydomain.com, that works great. 我在该计算机上安装了Nginx,并配置为使用redmine.home.mydomain.com和kb.home.mydomain.com,效果很好。 no issue when I type those in. 当我输入这些时没问题。

The problem I am having it when I type try to redirect to that URL from my external facing nginx server. 当我键入尝试从外部Nginx服务器重定向到该URL时遇到的问题。 I have redmine.mydomain.com and kb.mydomain.com. 我有redmine.mydomain.com和kb.mydomain.com。 The redmine one works great but the kb one redirects to redmine. Redmine一个很好用,但是kb One重定向到Redmine。 I can't figure out why. 我不知道为什么。

I have checked the response headers in several browsers and it gets redirected to https://kb.mydomain.com but the response from that call is the redmine home page. 我已经在多个浏览器中检查了响应头,并将其重定向到https://kb.mydomain.com,但该调用的响应是redmine主页。 Even though it should be redirecting to kb.home.mydomain.com which works fine if I type that in directly. 即使它应该重定向到kb.home.mydomain.com,如果我直接键入它也可以正常工作。

Bookstack config 书架配置

# redirect to ssl
server {
  listen 80;
  listen [::]:80;
  server_name kb.mydomain.com www.kb.mydomain.com;
  return 301 https://$server_name$request_uri;
}

server {
  listen 443 ssl;
  listen [::]:443 ssl;
  server_name kb.mydomain.com www.kb.mydomain.com;
  client_max_body_size 50M;


  ssl on;
  ssl_certificate /etc/letsencrypt/live/mydomain.com-0001/fullchain.pem;
  ssl_certificate_key /etc/letsencrypt/live/mydomain.com-0001/privkey.pem;

  access_log  /var/log/nginx/kb.access;
  error_log   /var/log/nginx/kb.error;

  location / {
    proxy_pass http://kb.home.mydomain.com/;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
  }
}

redmine config. Redmine的配置。

# redirect to ssl  
server {                      
  listen 80;
  listen [::]:80;
  server_name redmine.mydomain.com www.redmine.mydomain.com;
  return 301 https://$server_name$request_uri; 
}                                                                             

server {                                                                      
  listen 443 ssl http2;                                                       
  listen [::]:443 ssl http2;                                                  
  server_name redmine.mydomain.com www.redmine.mydomain.com;                  
  client_max_body_size 50M;                                                   
  ssl_certificate /etc/letsencrypt/live/mydomain.com-0001/fullchain.pem;      
  ssl_certificate_key /etc/letsencrypt/live/mydomain.com-0001/privkey.pem;    
  location / {
    proxy_pass http://redmine.home.mydomain.com;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr; 
  }                                                                           
}   

I am lost as to why it is redirecting to redmine any help would be greatly appreciated. 我不知道为什么它将重定向到Redmine,任何帮助将不胜感激。

It is happening cause you need to set the correct Header HTTP Host to works in your backend environment, how you did it wrong, you've even been inside the default website . 发生这种情况的原因是,您需要设置正确的Header HTTP Host才能在后端环境中工作,这是怎么做的,您甚至进入默认的网站。

server {
  listen 80;
  listen [::]:80;
  server_name kb.mydomain.com www.kb.mydomain.com;
  return 301 https://$server_name$request_uri;
}

server {
  listen 443 ssl;
  listen [::]:443 ssl;
  server_name kb.mydomain.com www.kb.mydomain.com;
  client_max_body_size 50M;


  ssl on;
  ssl_certificate /etc/letsencrypt/live/mydomain.com-0001/fullchain.pem;
  ssl_certificate_key /etc/letsencrypt/live/mydomain.com-0001/privkey.pem;

  access_log  /var/log/nginx/kb.access;
  error_log   /var/log/nginx/kb.error;

  location / {
    proxy_pass http://kb.home.mydomain.com/;
    proxy_set_header Host kb.home.mydomain.com;
    proxy_set_header X-Real-IP $remote_addr;
  }
}

redmine config. Redmine的配置。

server {                      
  listen 80;
  listen [::]:80;
  server_name redmine.mydomain.com www.redmine.mydomain.com;
  return 301 https://$server_name$request_uri; 
}                                                                             


server {                                                                      
  listen 443 ssl http2;                                                       
  listen [::]:443 ssl http2;                                                  
  server_name redmine.mydomain.com www.redmine.mydomain.com;                  
  client_max_body_size 50M;                                                   
  ssl_certificate /etc/letsencrypt/live/mydomain.com-0001/fullchain.pem;      
  ssl_certificate_key /etc/letsencrypt/live/mydomain.com-0001/privkey.pem;    
  location / {
    proxy_pass http://redmine.home.mydomain.com;
    proxy_set_header Host redmine.home.mydomain.com;
    proxy_set_header X-Real-IP $remote_addr; 
  }                                                                           
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM