Search code examples
linuxnginxredhat

Nginx redirection different with IP behavior on 2 different machine


context

I have 2 Linux Red Hat machines hosting nginx server. Nginx server is working with below config:

nginx config server 1

user1@server1:/etc/nginx/conf.d $ cat docgen_react.conf
server{
        listen 80;
        listen [::]:80;
        server_name url1.com;
        root /home/user1/gru/documentgenerator/react_client/build;
        index index.html index.htm;
}

nginx config server 2

user2@server2:/etc/nginx/conf.d $  cat docgen_react.conf
server{
        listen 80;
        listen [::]:80;
        server_name url2.com;
        root /export/home/user2/gru/documentgenerator/react_client/build;
        index index.html index.htm;
}

current behavior

  • If I am navigating to www.url1.com, it points properly to the webpage I want to reach hosted on server 1
  • If I am navigating to www.url2.com, it points properly to the webpage I want to reach hosted on server 2
  • If I am navigating to ip server 1, it points properly to the webpage I want to reach hosted on server1 (same as www.url1.com)
  • If I am navigating to ip server 2, I point to web page located on /usr/share/doc/HTML/en-US/index.html (not same as www.url2.com)

expected behavior

  • If I am navigating to www.url1.com, it points properly to the webpage I want to reach hosted on server 1
  • If I am navigating to www.url2.com, it points properly to the webpage I want to reach hosted on server 2
  • If I am navigating to ip server 1, it points properly to the webpage I want to reach hosted on server1 (same as www.url1.com)
  • If I am navigating to ip server 2, it points properly to the webpage I want to reach hosted on server2 (same as www.url2.com)

To sum-up if I use IP address I want to reach the same page as if I am navigating via www.url2.com


Solution

  • Thanks to @ThanhNguyenVan's comment, I found the solution.

    I was only focusing on /etc/nginx/conf.d and as I am not familiar with nginx I was not aware that there was a config above i.e /etc/nginx/nginx.conf. I then compare the server1 vs server2 config and saw the difference.

    enter image description here

    After commenting that part below it worked like charm:

      server {
          listen       80 default_server;
          listen       [::]:80 default_server;
          server_name  _;
          root         /usr/share/nginx/html;
    
          # Load configuration files for the default server block.
          include /etc/nginx/default.d/*.conf;
    
          location / {
          }
    
          error_page 404 /404.html;
              location = /40x.html {
          }
    
          error_page 500 502 503 504 /50x.html;
              location = /50x.html {
          }
      }