> For the complete documentation index, see [llms.txt](https://avincheng.gitbook.io/notebook/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://avincheng.gitbook.io/notebook/linux/nginx/nginx-pei-zhi-301-yong-jiu-zhong-ding-xiang.md).

# nginx 配置 301 永久重定向

## nginx 配置 301 永久重定向

将 HTTP 永久重定向到 HTTPS：

```
# redirect http to https
server {
  listen 80;
  server_name avincheng.com;
  return 301 https://avincheng.com$request_uri;
}
```

将带的 www 二级域名永久重定向到对应的一级域名：

```
# redirect www to root
server {
  server_name www.avincheng.com;
  return 301 $scheme://avincheng.com$request_uri;
}
```

重启 nginx 服务：

```
sudo systemctl restart nginx
```

## 参考文献

* [Converting rewrite rules](http://nginx.org/en/docs/http/converting_rewrite_rules.html)
