Weird problem with nginx matching home page
淡淡烟草味
淡淡烟草味 2017-05-16 17:28:20
0
4
528
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. .

淡淡烟草味
淡淡烟草味

reply all(4)
刘奇
location = / {
  rewrite / /index.html break;
  root /usr/share/nginx/html;
  index index.html;
}
伊谢尔伦

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

迷茫

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

给我你的怀抱

Replace the two locations. . .

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template