V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
edison111cry
V2EX  ›  问与答

3000 端口如何使用 https

  •  
  •   edison111cry · 2017-10-25 16:47:51 +08:00 · 8080 次点击
    这是一个创建于 2389 天前的主题,其中的信息可能已经有所发展或是发生改变。
    经过对 nginx 的配置实现了 HTTP 强制转为 HTTPS:访问
    http://www.example.com
    或者
    http://www.example.com:80
    都可以跳转到:

    https://www.example.com

    但是同时服务器上还有一个 NODE 程序在跑,访问:
    http://www.example.com:3000
    可以访问到,但是有没有办法
    https://www.example.com:3000
    也可以访问到呢?
    30 条回复    2019-04-01 16:18:45 +08:00
    yuxuan
        1
    yuxuan  
       2017-10-25 16:53:24 +08:00   ❤️ 1
    server
    {
    listen 443;
    server_name yoursite.com;
    index index.html index.htm index.php default.html default.htm default.php;

    include none.conf;
    #error_page 404 /404.html;
    include enable-php.conf;
    location ~ /*
    {
    proxy_pass http://127.0.0.1:3333;
    }
    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
    {
    expires 30d;
    }
    location ~ .*\.(js|css)?$
    {
    expires 12h;
    }
    location ~ /\.
    {
    deny all;
    }
    access_log off;
    }
    我 nginx 这么配的
    edison111cry
        2
    edison111cry  
    OP
       2017-10-25 17:01:35 +08:00
    @yuxuan 这样改的话可以实现

    https:可以 访问 NODE.JS 的 3000 应用了,但是这样就访问不了 80 端口里的 index.html 内容了。

    所以这个 HTTPS 能不能像 HTTP 实现以下两种:

    http :访问的是 80 端口的 index.html

    http:3000 : 访问的是 3000 端口的 node.js
    hzw758
        3
    hzw758  
       2017-10-25 17:06:45 +08:00
    类似问题我挣扎过很久,建议你把 nodejs 挂在二级域名上,一切都解决了
    edison111cry
        4
    edison111cry  
    OP
       2017-10-25 17:10:38 +08:00
    @hzw758 嗯,是的,挂在二级域名可以搞定的。
    然后想问一下这种情况 https 没办法像 http 那样通过增加一个端口号就可以访问到 80 又可以访问到 3000 端口是吧?
    Lentin
        5
    Lentin  
       2017-10-25 17:13:41 +08:00
    可以试试这个东西
    https://caddyserver.com
    ysc3839
        6
    ysc3839  
       2017-10-25 17:16:10 +08:00 via Android
    你的意思是 HTTP 和 HTTPS 两种协议共用一个端口吗?这个是可以实现,但 nginx 似乎不行。
    不过 nginx 能做到这样的效果:在 3000 开 https,然后用 http://host:3000 访问的时候自动跳转为 https://host:3000
    noe132
        7
    noe132  
       2017-10-25 17:18:46 +08:00 via Android
    没太看懂你的意思。。。
    你是说需要在
    80 监听 nginx http
    443 监听 nginx https
    3000 监听 node http 和 https 么?
    edison111cry
        8
    edison111cry  
    OP
       2017-10-25 17:22:18 +08:00
    @ysc3839 不是,我想问一下 https 是不是可以像 http 那样通过区分端口号来访问不同的应用。
    比如 http:80 这个访问的是 php,http:3000 这个访问的是 node.js

    那么 https 能这样吗?
    edison111cry
        9
    edison111cry  
    OP
       2017-10-25 17:22:37 +08:00
    @noe132 不是,我想问一下 https 是不是可以像 http 那样通过区分端口号来访问不同的应用。
    比如 http:80 这个访问的是 php,http:3000 这个访问的是 node.js

    那么 https 能这样吗?
    noe132
        10
    noe132  
       2017-10-25 17:32:50 +08:00 via Android
    @edison111cry
    肯定是可以的。而且这不关 https 的事,任何协议都可以监听任何端口(理论上)
    如果是光监听 http 很简单,1 是 node 直接监听 3000 端口,2 或者 node 监听一个本地端口,然后用 nginx 反代。

    https 的话多一个证书配置。在 nginx 配证书的话就在 conf 里添加一个 server,使用 nginx 配置的证书,反代到 node 的 http 端口上。或者是 node 编写配置 https 服务端。

    如果需要在 300 )配置 http 和 https,在 nginx 配置两个监听 3000 的 server 就行了。如果需要 http 跳转 https,就配一条 rewrite 即可。如果是用 node 的话那就得看 node 的 https 客户端怎么配置了
    xgfan
        11
    xgfan  
       2017-10-25 18:17:36 +08:00   ❤️ 1
    http,https 和端口没啥关系,只要你愿意,可以用 80 开 https,443 开 http。
    yuxuan
        12
    yuxuan  
       2017-10-25 18:21:38 +08:00
    @edison111cry 回答完就忘记这茬了 https 再去访问端口 这个还真没注意过 我自己是挂的 2 级域名
    xgfan
        13
    xgfan  
       2017-10-25 18:23:12 +08:00
    看了下,你希望
    http://a.com:80 nginx 301-> https://a.com:443
    https://a.com:443 nginx

    http://a.com:3000 node
    https://a.com:3000 nginx

    很简单啊。node 去监听 4000
    http://a.com:3000 nginx 301->https://a.com:3000
    https://a.com:3000 nginx 反代 4000
    Tink
        14
    Tink  
       2017-10-25 20:08:11 +08:00 via iPhone
    没问题
    edison111cry
        15
    edison111cry  
    OP
       2017-10-25 20:36:35 +08:00
    @xgfan
    大神,您説的就是我想要达成的效果。但是我还是有一点不知道咋配置,
    我现在的 nginx.conf 文件里,
    server {
    listen 80;
    rewrite ^(.*)$ https://$host$1 permanent;
    }
    server {
    listen 443 ssl;
    server_name localhost;
    }
    现在访问 http://a.com:80 可以实现跳转到 https://a.com

    但是 http://a.com:3000 现在访问不了了,没有配置 HTTPS 前是可以访问到 NODE 程序的

    所以我怎么样配置才能 http://a.com:3000 也跳转到 https://a.com:3000


    很简单啊。node 去监听 4000
    http://a.com:3000 nginx 301->https://a.com:3000
    https://a.com:3000 nginx 反代 4000

    您写的这段没能太明白,node 监听的是 3000,还要再去监听 4000 ?
    xfspace
        16
    xfspace  
       2017-10-25 20:58:58 +08:00 via Android
    server {
    listen 3000 ssl;
    }
    edison111cry
        17
    edison111cry  
    OP
       2017-10-25 21:09:42 +08:00
    @xfspace 第一次用 HTTPS,我还以为 HTTPS 只能监听 443 呢。
    刚监听 3000 端口成功了,但是
    https:/a.com:3000 却显示的是 80 端口的程序服务,怎么配置能让它显示 NODE 的 3000 程序
    Marfal
        18
    Marfal  
       2017-10-25 21:12:22 +08:00
    @edison111cry 求求你发个完整的配置吧
    edison111cry
        19
    edison111cry  
    OP
       2017-10-25 21:26:30 +08:00
    http {

    server {
    listen 80;
    server_name localhost;


    location / {
    root html;
    index index.php index.html index.htm;
    }

    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
    root html;
    }
    rewrite ^(.*)$ https://$host$1 permanent;

    location ~ \.php/?.*$ {
    root html;
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_index index.php;
    include fastcgi.conf;
    set $fastcgi_script_name2 $fastcgi_script_name;
    if ($fastcgi_script_name ~ "^(.+\.php)(/.+)$") {
    set $fastcgi_script_name2 $1;
    set $path_info $2;
    }
    fastcgi_param PATH_INFO $path_info;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name2;
    fastcgi_param SCRIPT_NAME $fastcgi_script_name2;
    }

    }



    server {
    listen 443 ssl;
    server_name localhost;

    ssl_certificate cert/214291770900892.pem;
    ssl_certificate_key cert/214291770900892.key;

    ssl_session_cache shared:SSL:1m;
    ssl_session_timeout 5m;
    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
    ssl_prefer_server_ciphers on;

    location / {
    root html;
    index index.php index.html index.htm;
    }
    location ~ \.php/?.*$ {
    root html;
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_index index.php;
    include fastcgi.conf;
    set $fastcgi_script_name2 $fastcgi_script_name;
    if ($fastcgi_script_name ~ "^(.+\.php)(/.+)$") {
    set $fastcgi_script_name2 $1;
    set $path_info $2;
    }
    fastcgi_param PATH_INFO $path_info;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name2;
    fastcgi_param SCRIPT_NAME $fastcgi_script_name2;
    }


    }

    # nginx listen on port 3000 with https
    server {
    listen 3000 ssl;
    server_name localhost:3000;

    ssl_certificate cert/214291770900892.pem;
    ssl_certificate_key cert/214291770900892.key;

    ssl_session_cache shared:SSL:1m;
    ssl_session_timeout 5m;
    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
    #ssl_ciphers HIGH:!aNULL:!MD5;
    ssl_prefer_server_ciphers on;
    }

    }
    edison111cry
        20
    edison111cry  
    OP
       2017-10-25 21:28:52 +08:00
    @Marfal 刚发出来了,不过没有了缩进,可能看得有点累。

    我的想法是实现 HTTPS 或者 HTTP 后面加:3000 都会跳转到 node 程序。
    Marfal
        21
    Marfal  
       2017-10-25 22:31:27 +08:00
    xfspace
        22
    xfspace  
       2017-10-26 00:03:05 +08:00 via Android
    @edison111cry 用 proxy_pass ?
    ysc3839
        23
    ysc3839  
       2017-10-26 03:01:10 +08:00 via Android
    @edison111cry 肯定可以的,https 又不强制 443 端口,你可以在 3000 端口上开 https。
    paranoiagu
        24
    paranoiagu  
       2017-10-26 08:20:19 +08:00 via Android
    3000 端口既是 http,又是 https,这怎么搞?
    ysc3839
        25
    ysc3839  
       2017-10-26 08:28:59 +08:00 via Android
    @paranoiagu 我不确定 nginx 能否实现。不过我前面说了,nginx 能实现一个类似的效果:在 3000 开 https,然后用 http//host:3000 访问的时候自动跳转为 https//host:3000
    msg7086
        26
    msg7086  
       2017-10-26 09:39:56 +08:00
    你这例子前后完全是两回事啊。

    你 nginx 上是访问 http 80 端口自动跳转到 https 443 端口。
    这是两个不同的服务跑在两个不同的端口上。
    一个是 80,一个是 443。

    现在你要 3000 端口既做 http 又做 https,这难度跟之前的可不是一个级别。
    edison111cry
        27
    edison111cry  
    OP
       2017-10-26 11:32:22 +08:00
    @msg7086 经过大家的回复,我意识到自已之前的理解有一定问题了。

    之前访问 http:/a.com:80 或者 http:/a.com 可以访问到 index.html 程序。
    访问 http:/a.com:3000 可以访问到 node.js 程序。

    然后我布薯了 HTTPS,让 HTTP 链接转为 HTTPS,80 端口没有问题,但是 3000 端口访问不到
    msg7086
        28
    msg7086  
       2017-10-26 11:37:53 +08:00
    80 端口部署 https 的话,是部署在 443 上,不是 80 上的。
    msg7086
        29
    msg7086  
       2017-10-26 11:39:28 +08:00
    也就是说,首先你要为你 nodejs 指定一个新的端口给 https 服务才好。
    openDatabase
        30
    openDatabase  
       2019-04-01 16:18:45 +08:00
    @edison111cry 现在怎么样了 问题解决了吗 我也遇到了同样的问题,现在网站访问使用 https http 访问会从重写成 https,还有一个链接是通过 8081 才能访问的 怎么让 8081 的访问也启动 https
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   3205 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 31ms · UTC 14:14 · PVG 22:14 · LAX 07:14 · JFK 10:14
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.