V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
yantianqi
V2EX  ›  程序员

nginx 下 https 下的 www 跳转不带 www

  •  
  •   yantianqi · 2018-12-11 10:01:01 +08:00 · 2928 次点击
    这是一个创建于 1934 天前的主题,其中的信息可能已经有所发展或是发生改变。
    14 条回复    2018-12-12 08:59:43 +08:00
    freshmanc
        1
    freshmanc  
       2018-12-11 10:13:05 +08:00
    CNAME 就能干这事了吧
    xiwangzishi
        2
    xiwangzishi  
       2018-12-11 10:18:13 +08:00
    ```nginx
    server{
    listen 443;
    server_name www.abc.com;
    return 301 https://abc.com$request_uri;
    }
    ```
    zpf124
        3
    zpf124  
       2018-12-11 10:19:28 +08:00
    server {
    listen 80 default_server;
    listen [::]:80 default_server;

    # Redirect all HTTP requests to HTTPS with a 301 Moved Permanently response.
    return 301 https://zfly.me$request_uri;
    }


    我是把所有的请求都转到不带 w 的上了, 你如果只需要把 www 转到不带 w 的上,那就把上面的哪个 defalut_server 删了,然后加一行 server_name www.example.com;
    yantianqi
        4
    yantianqi  
    OP
       2018-12-11 10:21:43 +08:00
    @xiwangzishi 对,网上搜索了老半天,突然想到 https 是监听 443,所以配置 443 跳转,是这样配置。
    yantianqi
        5
    yantianqi  
    OP
       2018-12-11 10:51:50 +08:00
    总结了一下配置 nginx 跳转过程[配置 nginx]( https://kkfor.com/article/5c0e8bc5ed10584962b64650)
    xpresslink
        6
    xpresslink  
       2018-12-11 11:10:51 +08:00
    直接在 DNS 上添加一条 CNAME 解析一下多好啊,为毛要这么脱了裤子放屁?
    jarry777
        7
    jarry777  
       2018-12-11 11:55:04 +08:00   ❤️ 3
    @xpresslink

    1、CNAME 不传递权重
    2、CNAME 只是解析,浏览器地址显示的仍然是原地址,在本例中,如果子域名和裸域名证书不同会报错

    张口就说一件事是脱裤子放屁只能暴露你的无知和素质低下
    xpresslink
        8
    xpresslink  
       2018-12-11 13:13:41 +08:00
    @jarry777 如果需要显示新地址, 现在 DNS 也都支持 301 重定向呀, 绝对比你自己搭的服务器可用性强.
    Timgle168
        9
    Timgle168  
       2018-12-11 15:42:06 +08:00
    配置 nginx 使它强制跳转到 https 就行
    lhx2008
        10
    lhx2008  
       2018-12-11 15:43:31 +08:00 via Android
    @xpresslink dns 301 一般不支持 https
    talas
        11
    talas  
       2018-12-12 08:24:41 +08:00 via iPhone
    RewriteEngine on
    RewriteBase /
    RewriteCond %{SERVER_PORT} !^443$
    RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [L,R]
    talas
        12
    talas  
       2018-12-12 08:27:30 +08:00 via iPhone
    呃 apache 下这样实现,找找在线工具可以转 nginx。
    talas
        13
    talas  
       2018-12-12 08:30:44 +08:00 via iPhone
    RewriteEngine on
    RewriteBase /
    RewriteCond %{HTTP_HOST} !^name.org [NC]
    RewriteRule ^(.*)$ http://name.org/$1 [L,R=301]
    RewriteCond %{SERVER_PORT} !^443$
    RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [L,R]
    这个是不带 www 且 http 跳转 https 的 apache 规则
    lichao
        14
    lichao  
       2018-12-12 08:59:43 +08:00


    仅供参考
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1481 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 17:22 · PVG 01:22 · LAX 10:22 · JFK 13:22
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.