我试图redirect在nginx中的http和https的根域到相同的子目录(https):
所以,例如
http://example.com -> https://example.com/subdirectory https://example.com -> https://example.com/subdirectory
我觉得这很简单,我正努力做到这一点。 我试过使用重写的变体,并返回301,但总是以redirect循环结束。
我当前的configuration(导致循环):
server { listen 80; server_name example.com; return 301 https://$server_name/subdirectory; } server { listen 443 ssl spdy; server_name example.com; return 301 https://$server_name/subdirectory; }
所以基本上,我试图redirect到https域根目录上相同的子目录,无论是通过http或https请求根域。
这个configuration会做你想要的:
server { listen 80: server_name example.com; return 301 https://$server_name/subdirectory; } server { listen 443; server_name example.com; location = / { return 301 https://$server_name/subdirectory; } }
= /说明符表示完全匹配,所以它只匹配虚拟服务器的确切根URI。
显然,如果您不从ssl虚拟主机的此行为中排除位置子目录,它将不起作用。
server { listen 80; server_name example.com; return 301 https://$server_name/subdirectory; } server { listen 443 ssl spdy; server_name example.com; location /subdirectory { # Your stuff } location = / { return 301 https://$server_name/subdirectory; } }
server { listen 80: server_name example.com; return 301 https://$server_name/subdirectory/; } server { listen 443; server_name example.com; location = / { return 301 https://$server_name/subdirectory/; } }
看看我如何在最后加上尾部的斜杠,这是非常重要的,否则你会得到redirect循环。