3000 端口如何使用 https

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

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

那么 https 能这样吗?
noe132
2017-10-25 17:32:50 +08:00
@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 客户端怎么配置了
6IbA2bj5ip3tK49j
2017-10-25 18:17:36 +08:00
http,https 和端口没啥关系,只要你愿意,可以用 80 开 https,443 开 http。
yuxuan
2017-10-25 18:21:38 +08:00
@edison111cry 回答完就忘记这茬了 https 再去访问端口 这个还真没注意过 我自己是挂的 2 级域名
6IbA2bj5ip3tK49j
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
2017-10-25 20:08:11 +08:00
没问题
edison111cry
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
2017-10-25 20:58:58 +08:00
server {
listen 3000 ssl;
}
edison111cry
2017-10-25 21:09:42 +08:00
@xfspace 第一次用 HTTPS,我还以为 HTTPS 只能监听 443 呢。
刚监听 3000 端口成功了,但是
https:/a.com:3000 却显示的是 80 端口的程序服务,怎么配置能让它显示 NODE 的 3000 程序
Marfal
2017-10-25 21:12:22 +08:00
@edison111cry 求求你发个完整的配置吧
edison111cry
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
2017-10-25 21:28:52 +08:00
@Marfal 刚发出来了,不过没有了缩进,可能看得有点累。

我的想法是实现 HTTPS 或者 HTTP 后面加:3000 都会跳转到 node 程序。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/400665

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX