astome
V2EX  ›  PHP

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

  •  
  •   astome · Sep 7, 2018 · 12193 views
    This topic created in 2982 days ago, the information mentioned may be changed or developed.

    我想把 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 replies  •  2018-09-07 21:58:49 +08:00
    AlphaTr
    AlphaTr
    Sep 7, 2018
    1
    我是这么配的,可以参考下:

    ```
    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
    MaiCong
    Sep 7, 2018
    2
    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;
    }
    }
    dimlau
    dimlau
    Sep 7, 2018
    4
    我的:

    ```
    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
    fmumu
    Sep 7, 2018 via Android
    5
    dns 别名 ➕http 转 https
    imn1
    imn1
    Sep 7, 2018
    6
    chrome 69 好像已经实现了
    Duolingo
    Duolingo
    Sep 7, 2018 via Android❤️1
    8
    不要乱用 xxx.com
    可以用 example.com
    IsA26hN4DcQDS7Z9
    IsA26hN4DcQDS7Z9
    Sep 7, 2018
    9
    用面板去吧,哈哈哈哈
    choicky
    choicky
    Sep 7, 2018 via iPhone
    10
    caddy 更简单。个人小站就 caddy 好了。
    lfzyx
    lfzyx
    Sep 7, 2018
    11
    用 return 301 是正确的做法,用 if 和 rewrite 是错误的做法
    Les1ie
    Les1ie
    Sep 7, 2018
    12
    @codingadog hhhhhhhhhhhhh 也曾尴尬过
    Topmax
    Topmax
    Sep 7, 2018
    13
    80 端口下添加 return 301 https://$server_name$request_uri;
    server_name 后面把所有域名加上
    ydxred
    ydxred
    Sep 7, 2018
    14
    @codingadog 哈哈哈哈哈 xxx.com 翻墙就知道是什么了
    Tink
    Tink
    Sep 7, 2018
    15
    cloudflare page_rules
    yxy2829
    yxy2829
    Sep 7, 2018
    17
    @ydxred 有新发现哈哈
    lzvezr
    lzvezr
    Sep 7, 2018 via Android
    18
    之前用跳转,现在直接关 80 端口
    Sharuru
    Sharuru
    Sep 7, 2018
    19
    糙快猛:
    1. 域名泛解析
    2. ACME 申请 Let's Encrypt,自动设置,完事儿(
    torment5524
    torment5524
    Sep 7, 2018
    20
    <html>
    <meta http-equiv="refresh" content="0;url=https://www.xxx.com/">
    </html>
    把另外两个页面的首页换成 index.html,内容换成上面的内容。。我是这么搞的。
    wwwiamdog
    wwwiamdog
    Sep 7, 2018
    22
    别这样我都点了。
    choicky
    choicky
    Sep 7, 2018
    23
    @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 了。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   956 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 87ms · UTC 19:26 · PVG 03:26 · LAX 11:26 · JFK 14:26
    ♥ Do have faith in what you're doing.