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

我想把 http://www.xxx.com ,http://xxx.com ,https://xxx.com 都指向到 https://www.xxx.com

  •  
  •   astome · 2018-09-07 10:28:54 +08:00 · 10238 次点击
    这是一个创建于 2263 天前的主题,其中的信息可能已经有所发展或是发生改变。

    我想把 http://www.xxx.com ,http://xxx.com ,https://xxx.com 都指向到 https://www.xxx.com

    nginx 怎么配置啊

    在网站找的方法 老是重定向次数过多!

    server{ listen 80; root /data/www/www.xxx.com; server_name www.xxx.com; }

    server { listen 443 ssl http2; #listen [::]:443 ssl http2; server_name www.xxx.com xxx.com; index index.html index.htm index.php; root /data/www/www.xxx.com; ssl on; ssl_certificate /usr/local/nginx/conf/ssl/xxx.com.crt; ssl_certificate_key /usr/local/nginx/conf/ssl/xxx.com.key; ssl_session_timeout 5m; ssl_protocols TLSv1 TLSv1.1 TLSv1.2; ssl_prefer_server_ciphers on; ssl_ciphers "EECDH+CHACHA20:EECDH+CHACHA20-draft:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256:EECDH+3DES:RSA+3DES:!MD5"; ssl_session_cache builtin:1000 shared:SSL:10m; # openssl dhparam -out /usr/local/nginx/conf/ssl/dhparam.pem 2048 ssl_dhparam /usr/local/nginx/conf/ssl/dhparam.pem;

        include rewrite/other.conf;
        #error_page   404   /404.html;
        # Deny access to PHP files in specific directory
        #location ~ /(wp-content|uploads|wp-includes|images)/.*\.php$ { deny all; }
    
        include enable-php-pathinfo.conf;
    
        location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
        {
            expires      30d;
        }
    
        location ~ .*\.(js|css)?$
        {
            expires      12h;
        }
    
        location ~ /.well-known {
            allow all;
        }
    
        location ~ /\.
        {
            deny all;
        }
    
        access_log  /home/wwwlogs/www.xxx.com.log;
    }
    
    23 条回复    2018-09-07 21:58:49 +08:00
    AlphaTr
        1
    AlphaTr  
       2018-09-07 10:33:31 +08:00
    我是这么配的,可以参考下:

    ```
    server {
    listen 80;
    server_name xxx.com www.xxx.com;
    return 301 https://www.xxx.com$request_uri;
    }
    server {
    listen 443 ssl http2;
    server_name xxx.com;

    ssl on;
    ssl_certificate fullchain.ecdsa-256.crt;
    ssl_certificate_key privkey.ecdsa-256.key;

    return 301 https://www.xxx.com$request_uri;
    }
    server {
    listen 443 ssl http2;
    server_name www.xxx.com;
    root /xxx/www;
    index index.html;
    ...
    }
    ```
    MaiCong
        2
    MaiCong  
       2018-09-07 10:34:47 +08:00
    server {
    listen 80;
    server_name xxx.com www.xxx.com;
    return 301 https://www.xxx.com$request_uri;
    }

    server {
    listen 443 ssl;
    server_name xxx.com www.xxx.com;
    if ($http_host != 'www.xxx.com' ) {
    return 301 https://www.xxx.com$request_uri;
    }
    }
    lzhd24
        3
    lzhd24  
       2018-09-07 10:39:33 +08:00 via Android
    dimlau
        4
    dimlau  
       2018-09-07 12:14:39 +08:00
    我的:

    ```
    server
    {
    listen 80;
    listen 443 ssl http2;
    server_name kaix.in *.kaix.in;
    index index.html index.php;
    root /home/wwwroot/kaixin;
    error_page 404 = /404.html;
    error_page 500 403 502 = /err.html;

    if ($host != 'kaix.in' ) {
    rewrite ^/(.*)$ https://kaix.in/$1 permanent;
    }

    if ($scheme != 'https' ) {
    rewrite ^/(.*)$ https://kaix.in/$1 permanent;
    }

    ...
    ...
    ...
    }
    ```
    fmumu
        5
    fmumu  
       2018-09-07 12:23:53 +08:00 via Android
    dns 别名 ➕http 转 https
    imn1
        6
    imn1  
       2018-09-07 12:23:54 +08:00
    chrome 69 好像已经实现了
    huaxing0211
        7
    huaxing0211  
       2018-09-07 12:29:39 +08:00
    codingadog
        8
    codingadog  
       2018-09-07 12:34:47 +08:00 via Android   ❤️ 1
    不要乱用 xxx.com
    可以用 example.com
    Raynard
        9
    Raynard  
       2018-09-07 12:36:38 +08:00
    用面板去吧,哈哈哈哈
    choicky
        10
    choicky  
       2018-09-07 13:23:29 +08:00 via iPhone
    caddy 更简单。个人小站就 caddy 好了。
    lfzyx
        11
    lfzyx  
       2018-09-07 13:25:13 +08:00
    用 return 301 是正确的做法,用 if 和 rewrite 是错误的做法
    Les1ie
        12
    Les1ie  
       2018-09-07 13:56:15 +08:00
    @codingadog hhhhhhhhhhhhh 也曾尴尬过
    h19981126g
        13
    h19981126g  
       2018-09-07 14:00:42 +08:00
    80 端口下添加 return 301 https://$server_name$request_uri;
    server_name 后面把所有域名加上
    ydxred
        14
    ydxred  
       2018-09-07 14:05:20 +08:00
    @codingadog 哈哈哈哈哈 xxx.com 翻墙就知道是什么了
    Tink
        15
    Tink  
       2018-09-07 14:06:18 +08:00
    cloudflare page_rules
    cncqw
        16
    cncqw  
       2018-09-07 14:28:30 +08:00
    yxy2829
        17
    yxy2829  
       2018-09-07 14:36:14 +08:00
    @ydxred 有新发现哈哈
    lzvezr
        18
    lzvezr  
       2018-09-07 14:41:33 +08:00 via Android
    之前用跳转,现在直接关 80 端口
    Sharuru
        19
    Sharuru  
       2018-09-07 14:44:56 +08:00
    糙快猛:
    1. 域名泛解析
    2. ACME 申请 Let's Encrypt,自动设置,完事儿(
    torment5524
        20
    torment5524  
       2018-09-07 14:47:57 +08:00
    <html>
    <meta http-equiv="refresh" content="0;url=https://www.xxx.com/">
    </html>
    把另外两个页面的首页换成 index.html,内容换成上面的内容。。我是这么搞的。
    wwwiamdog
        22
    wwwiamdog  
       2018-09-07 15:04:27 +08:00
    别这样我都点了。
    choicky
        23
    choicky  
       2018-09-07 21:58:49 +08:00
    @astome 安利一个 nginx 的替代品 ,caddy,安装过程见我的博文 https://itlaws.cn/post/caddy-installation-ubuntu/


    www.example.com {
    ...
    }

    example.com {
    redir https://www.example.com{uri}
    }

    第一个大括号,就能让 www.example.com 的 http 跳转到 https 了。
    第二个大括号,就能让 example.com 的 http/https 都跳转到 https://www.example.com 了。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   2779 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 20ms · UTC 08:00 · PVG 16:00 · LAX 00:00 · JFK 03:00
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.