• 首页
  • linux
  • nginx代理node.js服务器http路由强制跳转https

nginx代理node.js服务器http路由强制跳转https

image.png


文章目录



nginx反向代理node.js服务器


我们服务器上node.js部署的服务通常会用 pm2 或者 forever 等工具接管,
然后nginx反向代理对应node.js服务所绑定的端口.

server {
       listen        80;
       server_name  yijiebuyi.com;
       access_log  **这里是log日志目录**  main;
       client_max_body_size    50m;
       location / {
           root   html;
           expires -1;
           client_max_body_size    1000m;
           index  index.html index.htm;
           add_header Cache-Control no-store;
           proxy_pass http://127.0.0.1:3001;
           proxy_connect_timeout       600;
           proxy_send_timeout          600;
           proxy_read_timeout          600;
           send_timeout                600;
           proxy_redirect default;
           proxy_buffering off;
       proxy_set_header Host $host;
       proxy_set_header X-Real-IP $remote_addr;
       proxy_set_header X-Real-Port $remote_port;
       proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
       }
   }

注意:log日志目录处需要替换成自己服务器上存储log日志的实际路径

上面是 nginx目录下 config.d 目录下的配置文件,
博客是3001端口下启动的一个node.js服务,
nginx 反向代理3001端口然后转发到80端口,
我们的域名 https://yijiebuyi.com 解析指向服务器ip地址.



nginx配置https反向代理


首先你要有https证书

https不是一种新的协议,只是http的通信接口部分使用了ssl和tsl协议替代,加入了加密、证书、完整性保护的功能.


如何获得ssl或者tsl证书

回头会专门一篇博客来介绍免费申请https证书


nginx如何配置

当你已经为对应的域名申请到了相关证书,那么就可以接着做nginx的配置了

server {
   listen 443;
   server_name yijiebuyi.com;
   ssl on;
   root html;
   index index.html index.htm;
   ssl_certificate   /xxx/xxxx/xxxx_www.yijiebuyi.com.pem;
   ssl_certificate_key  /xxx/xxxx/xxxx_www.yijiebuyi.com.key;
   ssl_session_timeout 5m;
   ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
   ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
   ssl_prefer_server_ciphers on;
   location / {
       # add_header Access-Control-Allow-Origin *;
       proxy_pass   http://127.0.0.1:3001;
       proxy_set_header Host $host;
       proxy_set_header X-Real-IP $remote_addr;
       proxy_set_header X-Real-Port $remote_port;
       proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
   }
}

nginx反向代理node.js服务的3001端口然后转发到443端口,
注意:xxx代表我的证书存放路径和名称,copy使用时注意替换

443端口即网页浏览端口,主要是用于HTTPS服务,是提供加密和通过安全端口传输的另一种HTTP.


nginx强制https跳转http

当你的站点同时配置了https和http反向代理时,我们当然要是用https的协议更加安全,
所以日常的所有站点请求都希望强制跳转到https端口.


常规的rewrite跳转

域名通过正则匹配进行rewrite,然后以此来重定向至https的链接,
虽然这种方法很常见,但是nginx官方并不推荐是用此方法.

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

是用301重定向跳转

if ( $scheme = http ){
   return 301 https://$server_name$request_uri;
}

nginx支持这种语法,而且确实可以实现重定向,
但是官方依然不建议在配置文件中使用if语句做逻辑控制.

我们要在80端口配置项中直接用使用301跳转

server {
       listen        80;
       server_name  yijiebuyi.com;
       access_log  **这里是log日志目录**  main;
       client_max_body_size    50m;
       location / {
           root   html;
           expires -1;
           client_max_body_size    1000m;
           index  index.html index.htm;
           add_header Cache-Control no-store;
           proxy_pass http://127.0.0.1:3001;
           proxy_connect_timeout       600;
           proxy_send_timeout          600;
           proxy_read_timeout          600;
           send_timeout                600;
           proxy_redirect default;
           proxy_buffering off;
       proxy_set_header Host $host;
       proxy_set_header X-Real-IP $remote_addr;
       proxy_set_header X-Real-Port $remote_port;
       proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
       return 301 https://$server_name$request_uri;
       }
   }

注意:上面配置项的最后一行
当http请求落到3001服务上时,nginx转发此请求到https服务上.

出自:nginx代理node.js服务器http路由强制跳转https

回到顶部