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

问个 nginx http / https 的问题

  •  
  •   HLT · 2016-10-22 19:39:36 +08:00 · 3870 次点击
    这是一个创建于 2735 天前的主题,其中的信息可能已经有所发展或是发生改变。
    需求是:
    1 、 http 的 www 和 @ 强制跳转到 https://example.com
    2 、 https 的 www 也强制跳转到 https://example.com

    现在完成了 1 ,看了半天 nginx ,也没搞明白 2 怎么实现





    server {
    listen 80;
    server_name example.com www.example.com;
    return 301 https://example.com$request_uri;
    }


    server
    {
    #listen 80;
    listen 443 ssl http2;
    #listen [::]:80;

    ssl on;
    ssl_certificate /usr/local/nginx/conf/example.crt;
    ssl_certificate_key /usr/local/nginx/conf/example.key;

    server_name example.com www.example.com;
    index index.html index.htm index.php default.html default.htm default.php;
    root /home/wwwroot/example.com;
    include example.conf;
    #error_page 404 /404.html;
    include enable-php.conf;

    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
    {
    expires 30d;
    }

    location ~ .*\.(js|css)?$
    {
    expires 12h;
    }

    location ~ /\.
    {
    deny all;
    }

    access_log off;
    }
    17 条回复    2016-10-24 08:48:25 +08:00
    shierji
        1
    shierji  
       2016-10-22 19:57:38 +08:00 via Android
    类似第一个块 再写一个~
    haocity
        2
    haocity  
       2016-10-22 20:04:43 +08:00   ❤️ 1
    再写个 443 的 example.com 不就行了?
    server {
    listen 443 ssl http2;
    ssl on;
    ssl_certificate /usr/local/nginx/conf/example.crt;
    ssl_certificate_key /usr/local/nginx/conf/example.key;
    server_name example.com ;
    return 301 https://example.com$request_uri;
    }
    walkingway
        3
    walkingway  
       2016-10-22 20:07:43 +08:00
    server {
    listen 443;
    server_name www.example.com ;
    return 301 https://example.com$request_uri;
    }
    HLT
        4
    HLT  
    OP
       2016-10-22 20:23:56 +08:00
    @shierji
    @walkingway
    不行的
    xiaoc19
        5
    xiaoc19  
       2016-10-22 20:35:36 +08:00
    最近迷上 caddy 了,配置文件都不用指定就跳转
    Showfom
        6
    Showfom  
       2016-10-22 20:38:14 +08:00 via iPhone
    @HLT 没问题的 肯定可以的
    yytsjq
        7
    yytsjq  
       2016-10-22 20:42:28 +08:00
    这里有个方案,顺着配置很清晰的:

    https://bashy.im/blog/nginx-redirect-to-https-with-without-www-subdomain
    Patrick95
        8
    Patrick95  
       2016-10-22 23:55:14 +08:00
    我前几天刚好解决了这个问题,写一个带 www 的 https 配置

    server
    {
    listen 443;
    #listen [::]:80;
    ssl on;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_certificate /usr/local/nginx/ssl/fullchain.cer;
    ssl_certificate_key /usr/local/nginx/ssl/domain.me.key;
    server_name www.domain.com;
    return 301 https://domain.com$request_uri;

    }
    lslqtz
        9
    lslqtz  
       2016-10-23 02:43:41 +08:00
    这有多麻烦。。
    # HTTPS Config Start #
    #server {
    #listen 80;
    #server_name osu.pink www.osu.pink;
    #return 301 https://www.osu.pink$request_uri;
    #}
    #
    #server {
    #listen 443 ssl http2;
    #server_name osu.pink;
    #
    #ssl_prefer_server_ciphers on;
    #ssl_session_cache shared:SSL:10m;
    #ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    #ssl_ciphers EECDH+CHACHA20:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256:EECDH+3DES:RSA+3DES:!MD5;
    #ssl_certificate /usr/local/nginx/conf/1.crt;
    #ssl_certificate_key /usr/local/nginx/conf/1.key;
    #
    #return 301 https://www.osu.pink$request_uri;
    #}
    #
    #server {
    #listen 443 ssl http2;
    #server_name www.osu.pink;
    #index index.php index.htm index.html;
    #root /var/www/html/osu;
    #
    #ssl_prefer_server_ciphers on;
    #ssl_session_cache shared:SSL:10m;
    #ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    #ssl_ciphers EECDH+CHACHA20:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256:EECDH+3DES:RSA+3DES:!MD5;
    #ssl_certificate /usr/local/nginx/conf/1.crt;
    #ssl_certificate_key /usr/local/nginx/conf/1.key;
    #
    #location ~ .*\.(php|php5)?$ {
    #fastcgi_pass unix:/var/run/phpfpm.sock;
    #fastcgi_index index.php;
    #include fastcgi.conf;
    #}
    #}
    HLT
        10
    HLT  
    OP
       2016-10-23 04:30:58 +08:00
    @lslqtz
    @Patrick95
    @yytsjq
    @Showfom

    已经搞定了,谢谢大家
    HLT
        11
    HLT  
    OP
       2016-10-23 04:33:12 +08:00
    @haocity
    @walkingway
    @shierji

    谢谢大家
    shierji
        12
    shierji  
       2016-10-23 07:00:54 +08:00 via Android
    只需要像 walkingway 那样就行了 其他不用配置
    jackroyal
        13
    jackroyal  
       2016-10-23 14:31:36 +08:00 via Android
    @HLT 咋解决的,说下答案啊
    HLT
        14
    HLT  
    OP
       2016-10-23 15:47:56 +08:00
    @jackroyal 还是要单独填一个 443 的块,然后 server_name 是 www 的,指定 301 跳 https 的 @

    我之前也是按朋友告诉那么做的,只是单独填的那个 443 忘记加证书路径了。。。
    HLT
        15
    HLT  
    OP
       2016-10-23 15:49:09 +08:00
    @shierji 嗯,我提问题之前也是那么做的,只是忘记加证书路径了 所以不好使
    HLT
        16
    HLT  
    OP
       2016-10-23 15:50:37 +08:00
    @HLT

    完整配置






    server {
    listen 80;
    server_name example.com www.example.com;
    return 301 https://example.com$request_uri;
    }


    server {
    listen 443 ssl http2;
    ssl on;
    ssl_certificate /usr/local/nginx/conf/example.crt;
    ssl_certificate_key /usr/local/nginx/conf/example.key;
    server_name www.example.com;
    return 301 https://example.com$request_uri;
    }



    server
    {
    #listen 80;
    listen 443 ssl http2;
    #listen [::]:80;

    ssl on;
    ssl_certificate /usr/local/nginx/conf/example.crt;
    ssl_certificate_key /usr/local/nginx/conf/example.key;

    server_name example.com;
    index index.html index.htm index.php default.html default.htm default.php;
    root /home/wwwroot/example.com;
    include example.conf;
    #error_page 404 /404.html;
    include enable-php.conf;

    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
    {
    expires 30d;
    }

    location ~ .*\.(js|css)?$
    {
    expires 12h;
    }

    location ~ /\.
    {
    deny all;
    }

    access_log off;
    }
    smileawei
        17
    smileawei  
       2016-10-24 08:48:25 +08:00 via iPhone
    写两个 server

    一个把 www 和 @的 80 端口 301 到 https

    再写一个 https 的 server
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5217 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 30ms · UTC 08:43 · PVG 16:43 · LAX 01:43 · JFK 04:43
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.