Skip to content

Instantly share code, notes, and snippets.

@tarivs
Last active May 31, 2016 10:45
Show Gist options
  • Save tarivs/a118535f1e43c939386e249599e0dac8 to your computer and use it in GitHub Desktop.
Save tarivs/a118535f1e43c939386e249599e0dac8 to your computer and use it in GitHub Desktop.
301 Redirect in Nginx
实现301跳转
server {
listen 80;
server_name example.org;
return 301 $scheme://$host$request_uri;
}
其中以下为nginx全局变量:
$scheme : HTTP方法,如http,https;
$host : 请求主机头字段,否则为服务器名称;
$request_uri : 包含请求参数的原始URI,不包含主机名,如:”/foo/bar.php?arg=abc”;
$uri : 不带请求参数的当前URI,$uri不包含主机名,如”/foo/bar.html”。
可以通过替换以上的变量,实现很多功能。
实现www向非www跳转
server {
listen 80;
server_name www.example.org;
return 301 $scheme://example$request_uri;
}
实现80向443跳转
server {
listen 80;
server_name example.org;
return 301 https://$host$request_uri;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment