search

Home  >  Q&A  >  body text

Weird problem with nginx matching home page

upstream backend {
    server 192.168.0.100:80;
    server 192.168.0.100:81;
}
server {
    listen 80;
    server_name www.abc.com abc.com;
    root  /opt/wwwroot/abc.com/;

    location / {
        proxy_pass https://backend;
        proxy_set_header   Host   $host;
        proxy_set_header   X-Real-IP        $remote_addr;
        proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
        proxy_hide_header X-Powered-By;
    }

    location = / { 
        index index.html index.htm index.php;
    }
}

I want the user not to use proxy_pass when visiting www.abc.com/. Instead, they can directly access the local /opt/wwwroot/abc.com/index.html page, and all other requests will go through proxy_pass
I've been working on it for a long time and I can't figure it out. It's a little weird. Please help me. .

淡淡烟草味淡淡烟草味2897 days ago591

reply all(4)I'll reply

  • 怪我咯

    怪我咯2017-05-16 17:30:20

    location = / {
      rewrite / /index.html break;
      root /usr/share/nginx/html;
      index index.html;
    }
    

    reply
    0
  • 伊谢尔伦

    伊谢尔伦2017-05-16 17:30:20

    This is caused by the default location matching rules of nginx. The location matching of nginx matches relative URI. The location matching rules of nginx are as follows:

    1. First match "=", which is the so-called exact match
    2. Secondly, match regular expressions, such as "~" or "^~"
    3. Secondly, match according to the order of configuration files
    4. Finally, hand over to/for universal matching

    Understanding the location matching rules of nginx, your situation is easy to explain. The relative URI of www.abc.com/ is /. First, it accurately matches location = /. Other relative URIs such as www.abc.com/adf It is /adf, and it is handed over to universal matching according to your location matching rules

    If you want to solve this problem, just put the index in the universal matching. Writing a separate location = / {} has no effect according to your needs

    reply
    0
  • 迷茫

    迷茫2017-05-16 17:30:20

    If the location below is not needed, just index index.html; that’s it.

    reply
    0
  • 给我你的怀抱

    给我你的怀抱2017-05-16 17:30:20

    Replace the two locations. . .

    reply
    0
  • Cancelreply