我尝试仅重定向某些请求以使用 SSL,而不是重定向整个网站,但我不知道如何摆脱这种重定向循环。这是我的服务器块,我尝试使用位置块重定向流量:
server {
listen 80;
server_name example.com;
rewrite ^ http://www.example.com$request_uri? permanent;
}
server {
listen 80;
listen 443 ssl;
ssl_certificate /srv/ssl/sslchain.crt;
ssl_certificate_key /srv/ssl/example.com.key;
server_name www.example.com;
location / {
root /usr/local/bin/rails/example/public;
index index.html index.htm;
passenger_enabled on;
}
location /users/sign_up {
rewrite ^ https://www.example.com/users/sign_up break;
}
location /users/sign_in {
rewrite ^ https://www.example.com/users/sign_in break;
}
if ($host !~* ^(example.com|www.example.com)$){
return 502;
}
location ~* ^.+\.(jpg|jpeg|png|gif|css|js)$ {
root /usr/local/bin/rails/example/public;
passenger_enabled on;
expires 31d;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
我也尝试过“if”块......
if ($request_uri ~* /users/sign_up ) {
rewrite ^ https://www.example.com/users/sign_up break;
}
我已经尝试不使用“break”参数。如何才能重定向而不陷入重定向循环?谢谢。
答案1
当用户已经通过 SSL 连接时,您需要避免重定向。
像这样:
location /users/sign_up {
if ( $server_port = 80 ) {
rewrite ^ https://www.example.com/users/sign_up break;
}
}
location /users/sign_in {
if ( $server_port = 80 ) {
rewrite ^ https://www.example.com/users/sign_in break;
}
}