简体   繁体   中英

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. 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. I have nginx on that machine and configured to use redmine.home.mydomain.com and kb.home.mydomain.com, that works great. 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. I have redmine.mydomain.com and kb.mydomain.com. The redmine one works great but the kb one redirects to 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. Even though it should be redirecting to kb.home.mydomain.com which works fine if I type that in directly.

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.

# 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.

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 .

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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