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

nginx 怎么配置 www 域名跳非 www 域名呢?

  •  
  •   JunyuMu ·
    JunyuMu · 2016-03-04 02:05:15 +08:00 · 3952 次点击
    这是一个创建于 3190 天前的主题,其中的信息可能已经有所发展或是发生改变。

    搭了个 GHOST 博客,启用了 https 。
    现在想让 http 请求跳到 https ,并且 www 请求跳到非 www 上。应该怎么做呢??搜索了几个配置都有问题。。

    第 1 条附言  ·  2016-03-04 09:15:05 +08:00
    谢谢各位, 我在 stackoverflow 上找到了这个配置。原帖地址: http://stackoverflow.com/questions/7947030/nginx-no-www-to-www-and-www-to-no-www 顺便问下, 怎么支持 http2 ?

    #
    # Redirect all www to non-www
    #
    server {
    server_name www.example.com;
    ssl_certificate ssl/example.com/crt;
    ssl_certificate_key ssl/example.com/key;
    listen *:80;
    listen *:443 ssl spdy;
    listen [::]:80 ipv6only=on;
    listen [::]:443 ssl spdy ipv6only=on;

    return 301 https://example.com$request_uri;
    }

    #
    # Redirect all non-encrypted to encrypted
    #
    server {
    server_name example.com;
    listen *:80;
    listen [::]:80;

    return 301 https://example.com$request_uri;
    }

    #
    # There we go!
    #
    server {
    server_name example.com;
    ssl_certificate ssl/example.com/crt;
    ssl_certificate_key ssl/example.com/key;
    listen *:443 ssl spdy;
    listen [::]:443 ssl spdy;

    # rest goes here...
    }
    6 条回复    2016-03-04 12:20:05 +08:00
    dommyet
        1
    dommyet  
       2016-03-04 02:08:55 +08:00 via Android   ❤️ 1
    return 301 xxx.xxx.xxx
    shiny
        2
    shiny  
       2016-03-04 02:10:32 +08:00   ❤️ 1
    listen 80;
    listen 443 ssl;
    if ($server_port ~ 80) {
    return 301 https://example.com$request_uri;
    }

    https 跳转

    至于 www 的跳转 我是建一个 server_name 为 www.example.com ,然后 return 301
    你也可以对 $host 做判断再跳转。 google 搜下,很多 SO 的答案都有用。
    ryd994
        3
    ryd994  
       2016-03-04 04:05:01 +08:00 via Android   ❤️ 1
    @shiny if is evil
    三个 server 段
    一个 https example.com 做主要
    一个 https www.example.com 做 www 跳转
    一个 http 跳转
    ewBuyVmLZMZE
        4
    ewBuyVmLZMZE  
       2016-03-04 04:18:52 +08:00   ❤️ 1
    www 这个我直接在 DNS 上配置了解析跳转。

    然后 Nginx 配置 HTTP 跳转 HTTPS :

    server {
    listen 80;
    server_name xxx.xxx;
    location / {
    rewrite (.*) https://xxx.xxx$1 permanent;
    }
    }

    server {
    listen 443 ssl http2;
    server_name xxx.xxx;

    # let the browsers know that we only accept HTTPS
    add_header Strict-Transport-Security "max-age=63072000; includeSubdomains; preload";
    ssl on;
    ssl_certificate /path/to/crt;
    ssl_certificate_key /path/to/key;
    ssl_dhparam /path/to/pem;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_stapling on;
    ssl_ciphers "EECDH+AESGCM:EDH+AESGCM:ECDHE-RSA-AES128-GCM-SHA256:AES256+EECDH:DHE-RSA-AES128-GCM-SHA256:AES256+EDH:ECDHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES128-SHA256:ECDHE-RSA-AES256-SHA:ECDHE-RSA-AES128-SHA:DHE-RSA-AES256-SHA256:DHE-RSA-AES128-SHA256:DHE-RSA-AES256-SHA:DHE-RSA-AES128-SHA:ECDHE-RSA-DES-CBC3-SHA:EDH-RSA-DES-CBC3-SHA:AES256-GCM-SHA384:AES128-GCM-SHA256:AES256-SHA256:AES128-SHA256:AES256-SHA:AES128-SHA:DES-CBC3-SHA:HIGH:!aNULL:!eNULL:!EXPORT:!DES:!MD5:!PSK:!RC4";
    ssl_prefer_server_ciphers on;
    ssl_session_cache shared:SSL:10m;
    ssl_session_timeout 10m;

    location / {
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header Host $http_host;
    proxy_pass http://127.0.0.1:xxxx;
    }
    }
    Roope
        5
    Roope  
       2016-03-04 09:15:19 +08:00
    server {
    server_name xxx.com;
    return 301 http://www.xxx.com;
    }

    域名和请求方式可以用变量替换。
    popu111
        6
    popu111  
       2016-03-04 12:20:05 +08:00 via Android
    spdy 换 http2 ,另外懒得折腾还是上 typecho 或者 wp 这种吧
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   5385 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 01:19 · PVG 09:19 · LAX 17:19 · JFK 20:19
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.