Heim  >  Fragen und Antworten  >  Hauptteil

Seltsames Problem mit der passenden Nginx-Homepage

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

Ich möchte, dass Benutzer beim Besuch von www.abc.com/ nicht Proxy_pass verwenden. Stattdessen können sie direkt auf die lokale Seite /opt/wwwroot/abc.com/index.html zugreifen und alle anderen Anfragen werden über Proxy_pass weitergeleitet Ich habe lange daran gearbeitet und komme nicht dahinter. Bitte helfen Sie mir. .

淡淡烟草味淡淡烟草味2712 Tage vor505

Antworte allen(4)Ich werde antworten

  • 怪我咯

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

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

    Antwort
    0
  • 伊谢尔伦

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

    这是nginx的默认location匹配规则导致的,nginx的location匹配的是相对URI,nginx的location匹配规则如下:

    1. 首先匹配“=”,也就是所谓的精确匹配
    2. 其次,匹配正则表达式,例如"~"或者"^~"
    3. 再其次,按照配置文件的顺序进行匹配
    4. 最后,交给/进行通用匹配

    理解了nginx的location匹配规则,你的情况就很容易解释了,www.abc.com/的相对URI时/,首先时精确匹配location = / ,其他例如www.abc.com/adf的相对URI是/adf,根据你的location匹配规则交给了通用匹配

    想解决这个问题将index放在通用匹配里就行了,单独写一个location = / {}从你的需求来看没有什么作用

    Antwort
    0
  • 迷茫

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

    下面的location不需要的,直接在上面 index index.html; 这样就行了

    Antwort
    0
  • 给我你的怀抱

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

    把两个location换下位置。。。

    Antwort
    0
  • StornierenAntwort