V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
NGINX
NGINX Trac
3rd Party Modules
Security Advisories
CHANGES
OpenResty
ngx_lua
Tengine
在线学习资源
NGINX 开发从入门到精通
NGINX Modules
ngx_echo
70599
V2EX  ›  NGINX

nginx 怎样把 www.example.com 重定向到 example.com 上?

  •  
  •   70599 · 2015-11-23 23:44:28 +08:00 · 2811 次点击
    这是一个创建于 3069 天前的主题,其中的信息可能已经有所发展或是发生改变。
    4 条回复    2015-11-24 12:25:14 +08:00
    msg7086
        1
    msg7086  
       2015-11-23 23:46:52 +08:00
    server_name www.example.com;
    rewrite ^ http://example.com;
    UtopiaCHN
        2
    UtopiaCHN  
       2015-11-23 23:48:57 +08:00
    应该在域名解析上设置吧,最开始不是只有 http://example.com 吗,我是自己加的 www.example.com
    imkh
        3
    imkh  
       2015-11-24 00:06:28 +08:00   ❤️ 3
    server {
    listen 80;
    server_name www.old-name.com;
    return 301 $scheme://www.new-name.com$request_uri;
    }

    官方推荐用法

    https://www.nginx.com/blog/creating-nginx-rewrite-rules/#gs.BtHsKaw
    ychongsaytc
        4
    ychongsaytc  
       2015-11-24 12:25:14 +08:00
    3 楼正解。
    条件跳转不是个好办法。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3520 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 30ms · UTC 04:55 · PVG 12:55 · LAX 21:55 · JFK 00:55
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.