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

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

  •  
  •   viamcc · 2021-10-13 11:55:08 +08:00 · 1912 次点击
    这是一个创建于 898 天前的主题,其中的信息可能已经有所发展或是发生改变。
    目前的问题是 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
    gengchun  
       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   ·   我们的愿景   ·   实用小工具   ·   3403 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 10:38 · PVG 18:38 · LAX 03:38 · JFK 06:38
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.