Jak przekierować adres url w NGINX
I need to redirect every http://test.com wniosek do http://www.test.com . Jak można to zrobić.
W bloku serwera próbowałem dodać
rewrite ^/(.*) http://www.test.com/$1 permanent;
Ale w przeglądarce jest napisane
The page isn't redirecting properly
Firefox has detected that the server is redirecting the request for
this address in a way that will never complete.
Mój blok serwera wygląda jak
server {
listen 80;
server_name test.com;
client_max_body_size 10M;
client_body_buffer_size 128k;
root /home/test/test/public;
passenger_enabled on;
rails_env production;
#rewrite ^/(.*) http://www.test.com/$1 permanent;
#rewrite ^(.*)$ $scheme://www.test.com$1;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
3 answers
Najlepszym sposobem na zrobienie tego, co chcesz, jest dodanie kolejnego bloku serwera:
server {
#implemented by default, change if you need different ip or port
#listen *:80 | *:8000;
server_name test.com;
return 301 $scheme://www.test.com$request_uri;
}
I edytuj zmienną server_name bloku głównego w następujący sposób:
server_name www.test.com;
Ważne : nowy server
Blok jest właściwym sposobem, aby to zrobić, if
jest złem . Musisz używać lokalizacji i serwerów zamiast if
, jeśli to możliwe. Rewrite
jest czasem także złe , więc zastąpiono je return
.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-02-18 02:21:50
Podobne do innej odpowiedzi tutaj, ale zmień http w rewrite na $schemat TAK:
server {
listen 80;
server_name test.com;
rewrite ^ $scheme://www.test.com$request_uri? permanent;
}
I edytuj zmienną server_name bloku głównego w następujący sposób:
server_name www.test.com;
Musiałem to zrobić, aby przekierować www.test.com na test.com.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-07-26 18:17:56
Najpierw upewnij się, że zainstalowałeś Nginx z modułem http rewrite. Aby to zainstalować musimy mieć pcre-library
Jak zainstalować bibliotekę pcre
Jeśli już je posiadasz, po prostu dodaj poniższy kod do swojego bloku serwerów nginx if ($host !~* ^www\.) {
rewrite ^(.*)$ http://www.$host$1 permanent;
}
Aby usunąć www z każdego żądania możesz użyć
if ($host = 'www.your_domain.com' ) {
rewrite ^/(.*)$ http://your_domain.com/$1 permanent;
}
Więc twój blok serwera będzie wyglądał jak
server {
listen 80;
server_name test.com;
if ($host !~* ^www\.) {
rewrite ^(.*)$ http://www.$host$1 permanent;
}
client_max_body_size 10M;
client_body_buffer_size 128k;
root /home/test/test/public;
passenger_enabled on;
rails_env production;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-04-24 10:20:33