2012-12-19 14 views
6

Moje pliki na dysku mają rozszerzenia: index.html, a.html. Chcę, aby żądanie dla http://example.com/a załadować /var/www/a.html i http://example.com/ załadować /var/www/index.html. Chcę, aby każdy inny adres URL przekierowywał do kanonicznego adresu URL, więc http://example.com/a.html powinien przekierować na numer http://example.com/a.przekierowanie /foo.html do/foo ale nie/do/indeksu w nginx

Moja konfiguracja wygląda następująco:

rewrite ^(/.+)\.html$ $scheme://$host$1 permanent; 
location/{ 
    root /var/www; 
    try_files $uri.html $uri $uri/ =404; 
} 

ten sposób przekierować /a.html do /a i odnieść sukces przy załadunku a.html z dysku:

$ curl -D- -s http://www.jefftk.com/food.html | grep ^Location 
Location: http://www.jefftk.com/food 
$ curl -s http://www.jefftk.com/food | grep ^Location 

Ale wysyła / do /index:

$ curl -s -D- http://www.jefftk.com/pictures/ | grep ^Location 
Location: http://www.jefftk.com/pictures/index 
$ curl -s -D- http://www.jefftk.com | grep ^Location 
Location: http://www.jefftk.com/index 

Jeśli usunąć regułę przepisywania przestaje przekierowanie z /a.html do /a ale również przestanie wysyłać / do /index:

$ curl -D- -s http://www.jefftk.com/food.html | grep ^Location 
$ curl -D- -s http://www.jefftk.com/food | grep ^Location 
$ curl -D- -s http://www.jefftk.com/ | grep ^Location 
$ curl -D- -s http://www.jefftk.com/pictures/ | grep ^Location 

Dlaczego tak się stało? Czy mogę nginx do obu rzeczy, które chcę (nie .html rozszerzenie, no index w url) w tym samym czasie?

Odpowiedz

1

Myślę, że twoja reguła przepisywania może być cofnięta. Może to po prostu (nie reguła przepisywania):

location/{ 
    try_files $uri.html $uri $uri/ =404; 
} 

location =/{ 
    index index.html; 
} 

edytowanej wersji:

Niestety, ja nie rozumiejąc swój opis całkowicie. I ponownie przeczytać go kilka razy i przetestowane to i to może być zamknięcie do tego, czego szukasz zrobić:

location =/{ 
    try_files /index.html =404; 
} 

location = /index { 
    return 301 $scheme://$host; 
} 

location ~* \.html$ { 
    rewrite ^(.+)\.html$ $scheme://$host$1 permanent; 
} 

location/{ 
    try_files $uri.html $uri/ @backend; 
} 

location @backend { 
    # rewrite or do whatever is default for your setup 
    rewrite^/index.html last; 
    // or return 404; 
} 

przykładowy kod (rewizja 3):

Mam nadzieję, że po raz trzeci jest urok. Może to rozwiąże twój problem?

# example.com/index gets redirected to example.com/ 

location ~* ^(.*)/index$ { 
    return 301 $scheme://$host$1/; 
} 

# example.com/foo/ loads example.com/foo/index.html 

location ~* ^(.*)/$ { 
    try_files $1/index.html @backend; 
} 

# example.com/a.html gets redirected to example.com/a 

location ~* \.html$ { 
    rewrite ^(.+)\.html$ $scheme://$host$1 permanent; 
} 

# anything else not processed by the above rules: 
# * example.com/a will load example.com/a.html 
# * or if that fails, example.com/a/index.html 

location/{ 
    try_files $uri.html $uri/index.html @backend; 
} 

# default handler 
# * return error or redirect to base index.html page, etc. 

location @backend { 
    return 404; 
} 
+0

To jednak nie przekieruje 'http: // example.com/file.html' do' http: // example.com/file'. –

+0

Niestety, nie do końca zrozumiałem twoje pytanie, ale przeczytałem je ponownie i przetestowałem nowe zasady. Skorygowałem powyższy kod, aby uwzględnić dodatkowe lokalizacje. – kchan

+0

To też nie działa: podczas/nie przekierowuje do/index,/foo nadal przekierowuje do/foo/index –

0

Czy szukasz czegoś takiego:

location/{ 
    try_files $uri.html $uri/index.html =404; 
} 

Zasadniczo będzie to spróbuj plik a.html pierwsze, jeśli nie będzie to spróbuj index.html i ostatni wykazują 404. Pamiętaj także, aby po edycji pliku restart nginx dokonać edycji.

Powiązane problemy