回答
if ($host ~* www\.(.*)) {
set $host_without_www $1;
rewrite ^(.*)$ http://$host_without_www$1 permanent; # $1 contains '/foo', not 'www.mydomain.com/foo'
}
答: https://serverfault.com/questions/139579/nginx-subdomain-rewrite
不推荐使用这种方法现在。参见[关于常见陷阱的这一部分](http://wiki.nginx.org/Pitfalls#Using_If)。 – 2013-01-12 21:30:59
推荐的方法是什么? – Tony 2013-01-15 14:00:57
server {
listen 80;
listen 443;
server_name ~^www\.(\w+)\.domain\.com$;
location/{
rewrite^$scheme://$1.domain.com$request_uri? permanent;
}
}
请注意,这会产生一个双查询字符串,对'/?foo = bar'的请求将重定向到'/?foo = bar?foo = bar'。使用'$ uri'似乎按预期工作,但可能有更好的选择。 – sapht 2012-08-11 18:37:32
添加? $ request_uri之后将避免重复查询字符串问题。 – tarkeshwar 2012-09-01 20:00:32
server_name后面缺少分号 – alvin 2013-05-23 01:03:18
- 1. nginx的子域重写
- 2. 复杂nginx的重写规则子域
- 3. 重写一个子域蒙山nginx的
- 4. 如何使用nginx重写子域?
- 5. nginx通配符子域ssl重写
- 6. 用nginx和htaccess重写Wordpress子域名
- 7. Nginx的重写子域网址顶级域
- 8. Nginx重定向www.X.domain.com子域
- 9. 将位置重写为nginx中的子域
- 10. 将nginx中的子域url重写为后端服务器
- 11. 请帮我理解子域robots.txt的简单nginx重写问题
- 12. Nginx的:NONWWW(80)和重写一个子域(443)
- 13. 重写CNAME子域
- 14. htaccess重写:子域?
- 15. URL重写子域
- 16. Nginx重写(小写和子串)
- 17. 将子文件夹重写为子域而不用nginx重定向
- 18. 重写规则的子域
- 19. 的.htaccess重写子域
- 20. 子域的重写规则
- 21. htaccess的子域重写
- 22. nginx将所有通配符子域重写为www.site.com
- 23. Nginx的 - 重写/子文件夹
- 24. htaccess的重写辅助域子域
- 25. Nginx重定向*:端口到子域
- 26. nginx重写为htaccess重写
- 27. Nginx重写URL以包含通配符子域并同时更改域后缀
- 28. Nginx的URL重写
- 29. Nginx的URL重写
- 30. nginx与重写
类似的问题:http://stackoverflow.com/questions/2498712/nginx-subdomain-rewrite – 2010-05-13 07:32:18