
Я изменил свой файл conf так, чтобы при вводе пользователем домена без www он перенаправлялся на домен с www:
имя_сервера example.com; return 301 $scheme://www.example.com$request_uri; Я также хочу, чтобы мой https был для всего, что находится в /user
Я получаю сообщение об ошибке «слишком много перенаправлений». Где я ошибаюсь?
Так что я:
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /var/www/example.com/site;
index index.html index.htm;
# Make site accessible from http://localhost/
server_name example.com;
return 301 $scheme://www.example.com$request_uri;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
# Uncomment to enable naxsi on this location
# include /etc/nginx/naxsi.rules
}
location /user {
rewrite ^ https://$http_host$request_uri? permanent;
}
}
Для порта 443:
server {
listen 443;
server_name example.com;
return 301 $scheme://www.example.com$request_uri;
root /var/www/example.com/site;
index index.html index.htm;
ssl on;
ssl_certificate //path here
ssl_certificate_key //path here
location / {
rewrite ^ http://$http_host$request_uri? permanent;
}
location /user {
}
}
решение1
Лучший способ сделать это — иметь отдельные server
блоки для example.com
и www.example.com
. Запись для example.com
должнатолькоиметь:
server {
listen 80;
server_name example.com;
return 301 http://www.example.com$request_uri;
}
Запись для www.example.com
будет содержать все остальное (и, очевидно,нетиметь перенаправление).
Аналогично у вас будет две отдельные записи сервера для https (порт 443).
решение2
Я бы сделал что-то вроде этого: (не проверено)
server {
listen 80;
server_name example.com;
return 301 http://www.example.com$request_uri;
}
server {
listen 80;
server_name www.example.com;
root /var/www/example.com/site;
index index.html index.htm;
location / {
rewrite ^/index.php;
}
}
server {
listen 443;
server_name www.example.com;
root /var/www/example.com/site;
index index.html index.htm;
ssl on;
ssl_certificate //path here
ssl_certificate_key //path here
location /user {
rewrite ^ https://$http_host$request_uri? permanent;
}
}