Nginx: Umschreiben von der Basisdomäne in ein Unterverzeichnis

Nginx: Umschreiben von der Basisdomäne in ein Unterverzeichnis

Ich habe ein Blog, das gehostet wird beihttp://site.com/blog.

site.comWie weise ich Nginx an, Anfragen von nach umzuschreiben site.com/blog?

Dies sollte nicht dauerhaft sein.

Antwort1

location = / {
    rewrite ^ http://site.com/blog/ redirect;
}

Dies führt nur Anfragen speziell für die Wurzel aus. Wenn Sie alles abfangen müssen (Umleitung http://site.com/somearticle/something.htmlzu http://site.com/blog/somearticle/something.html), dann benötigen Sie etwas Komplizierteres:

location /blog/ {
    # Empty; this is just here to avoid redirecting for this location,
    # though you might already have some config in a block like this.
}
location / {
    rewrite ^/(.*)$ http://site.com/blog/$1 redirect;
}

Antwort2

versuchen Sie stattdessen Folgendes:

location = / {
      return 301 /blog/;
 }

Der Schlüssel hier ist'='Symbol.

Antwort3

Das hat bei mir nicht funktioniert. Das hier hat funktioniert:

  1. Öffnen Sie die NGINX-Konfigurationsdatei für Ihre Site. Fügen Sie im Serverblock den Pfad zu Ihrem Stammverzeichnis hinzu und legen Sie die Prioritätsreihenfolge für Dateien fest:

    root /mnt/www/www.domainname.com;
    index  index.php index.html index.htm;
    
  2. Einen leeren Standortblock erstellenVoralle Ihre anderen Standortblöcke:

    location /latest {
    # Nothing in here; this is to avoid redirecting for this location
    }
    
  3. Kommentieren Sie die Direktive zum Stammverzeichnis in Ihrem Block „location / {}“ aus und fügen Sie die Umleitung hinzu, sodass sie folgendermaßen aussieht:

    location / {
    # root   /mnt/www/www.domainname.com;
    index  index.php index.html index.htm;
    rewrite ^/(.*)$ http://www.domainname.com/latest/$1 redirect;
    }
    
  4. Stellen Sie sicher, dass Ihr location ~ .php$ Block seine Wurzel auf

    root /mnt/www/www.domainname.com;
    

Dadurch wurde das Problem für mich behoben.

Antwort4

nur für die Homepage und sonst nichts würde ich verwenden:

location / {           
    rewrite ^/$ /blog/ redirect;
}

alles andere, beispielsweise /foo/, würde nicht zu /blog/ umgeleitet.

verwandte Informationen