请教各位大佬一个 nginx 配置问题 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
viamcc
V2EX    NGINX

请教各位大佬一个 nginx 配置问题

  •  
  •   viamcc 2021-10-13 11:55:08 +08:00 2628 次点击
    这是一个创建于 1510 天前的主题,其中的信息可能已经有所发展或是发生改变。
    目前的问题是 http://example.com 可以正常跳转到 https://example.com ,而直接访问 http://www.example.com chrome 接下载空文件,safari 则无法访问。

    请教下各位大佬如何解决 www 这个跳转 https 的问题?

    另外有关 http 跳转 https 还有更简洁的配置方法吗?



    nginx version: nginx/1.20.1


    配置如下:

    server
    {
    listen 80 http2;
    server_name example.com wwww.example.com;

    return 301 https://$server_name$request_uri;
    # rewrite ^(.*)$ https://$host$1 permanent;

    location ~ /
    {
    root /home/wwwroot/serverhome;
    index index.html index.htm;
    }

    }

    server
    {
    listen 443 ssl http2;
    server_name example.com www.example.com;

    ssl_certificate /usr/local/nginx/ssl/example.com.crt;
    ssl_certificate_key /usr/local/nginx/ssl/example.com.key;
    ssl_session_timeout 5m;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
    ssl_prefer_server_ciphers on;

    root /home/wwwroot/serverhome;
    index index.htm index.html;

    access_log /home/wwwlogs/example.com.log main;

    }
    第 1 条附言    2021-10-13 16:31:30 +08:00
    感谢 @Arumoh 大佬提供线索 80 去掉 http2 就可以了。

    结帖。
    14 条回复    2021-10-16 13:18:21 +08:00
    amrnxcdt
        1
    amrnxcdt  
       2021-10-13 12:04:06 +08:00 via Android
    return 301 https://$server_name$request_uri;


    换成下面的


    rewrite ^(.*)$ https://$host$1 permanent;


    试试
    meshell
        2
    meshell  
       2021-10-13 12:04:22 +08:00
    wwww.example.com 四个 wwww 是几个意思?
    viamcc
        3
    viamcc  
    OP
       2021-10-13 12:05:38 +08:00
    @meshell 发帖子的手手滑编辑多了一个 w,实际没有的哈
    gengchun
        4
    gegchun  
       2021-10-13 12:08:13 +08:00
    @amrnxcdt $host 只是第一个 $server_name,这个要看具体需求吧,假如是想把 www 去掉的话,倒是可以。但是这个问题里没有这么描述。而且他这个配置里也注释掉了。
    AllenHua
        5
    AllenHua  
       2021-10-13 12:15:53 +08:00 via iPhone
    域名 http 转 https 建议这样写 `return 301 https://$host$request_uri;`
    xy90321
        6
    xy90321  
       2021-10-13 13:04:02 +08:00 via iPhone
    域名解析里面配了 www 或者 * 了吗?
    都没到 nginx 的话怎么改配置都没用
    ElmerZhang
        8
    ElmerZhang  
       2021-10-13 15:09:26 +08:00
    试试这个配置
    ```
    server {
    listen 80;
    server_name example.com wwww.example.com;

    return 301 https://$host$request_uri;
    }

    server {
    listen 443 ssl http2;
    server_name example.com www.example.com;

    ssl_certificate /usr/local/nginx/ssl/example.com.crt;
    ssl_certificate_key /usr/local/nginx/ssl/example.com.key;
    ssl_session_timeout 5m;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
    ssl_prefer_server_ciphers on;

    root /home/wwwroot/serverhome;
    index index.htm index.html;

    access_log /home/wwwlogs/example.com.log main;
    }
    ```
    Arumoh
        9
    Arumoh  
       2021-10-13 15:21:19 +08:00
    80 端口不要走 HTTP2
    huangzxx
        10
    huangzxx  
       2021-10-13 16:02:25 +08:00
    server {
    listen 80;
    listen [::]:80;
    server_name example.com
    www.example.com;
    return 301 https://$host$request_uri;
    }
    superrichman
        11
    superrichman  
       2021-10-13 16:10:57 +08:00
    error_page 497 https://$host$uri?$args;

    利用 http 497 状态码 强制跳转到 https

    (要啥 http,我嫌麻烦直接关掉了 80 端口
    viamcc
        12
    viamcc  
    OP
       2021-10-13 16:30:01 +08:00
    @Arumoh 果然 80 去掉 http2 可以了,神奇。 谢谢老哥
    amrnxcdt
        13
    amrnxcdt  
       2021-10-16 03:23:19 +08:00   1
    @gengchun #4 楼主已经解决了但是还是纠正一下,$server_name 才是在 server_name 指令中配置的第一个域名。

    server_name 指令指定多个域名的时候应该用$host 来获取正确的主机名,参考 8#和 10#的重写规则。

    因为楼主多域名配置而且是第二个域名访问出现问题,我一开始认为是变量问题。

    文档在 http://nginx.org/en/docs/http/ngx_http_core_module.html 的 Embedded Variables 节。

    相关讨论 : https://serverfault.com/questions/706438/what-is-the-difference-between-nginx-variables-host-http-host-and-server-na
    gengchun
        14
    gengchun  
       2021-10-16 13:18:21 +08:00
    @amrnxcdt 你是对的,这个我记错了。其实我一直用的 $http_host,因为很多时候,需要加上端口。这个就只有一个印象。
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     1271 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 23ms UTC 23:55 PVG 07:55 LAX 15:55 JFK 18:55
    Do have aith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86