关于全栈项目【臻美Chat】https访问 遇到的问题【技术栈:Nodejs】

r囧r小猫 2021-10-01 07:50 373阅读 0赞

首先我上线时可以http访问也可以https访问,那么配置如下:
nginx.conf

  1. user root;
  2. worker_processes auto;
  3. error_log /var/log/nginx/error.log;
  4. pid /run/nginx.pid;
  5. # Load dynamic modules. See /usr/share/nginx/README.dynamic.
  6. include /usr/share/nginx/modules/*.conf;
  7. events {
  8. worker_connections 1024;
  9. }
  10. http {
  11. log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  12. '$status $body_bytes_sent "$http_referer" '
  13. '"$http_user_agent" "$http_x_forwarded_for"';
  14. access_log /var/log/nginx/access.log main;
  15. sendfile on;
  16. tcp_nopush on;
  17. tcp_nodelay on;
  18. keepalive_timeout 65;
  19. types_hash_max_size 2048;
  20. include /etc/nginx/mime.types;
  21. default_type application/octet-stream;
  22. # Load modular configuration files from the /etc/nginx/conf.d directory.
  23. # See http://nginx.org/en/docs/ngx_core_module.html#include
  24. # for more information.
  25. include /etc/nginx/conf.d/*.conf;
  26. server {
  27. listen 80 default_server;
  28. listen [::]:80 default_server;
  29. listen 443 ssl ;
  30. server_name '域名';
  31. #ssl on;
  32. ssl_certificate cert/1_www.maomin.club_bundle.crt;
  33. ssl_certificate_key cert/2_www.maomin.club.key;
  34. ssl_session_timeout 5m;
  35. ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
  36. ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
  37. ssl_prefer_server_ciphers on;
  38. # Load configuration files for the default server block.
  39. include /etc/nginx/default.d/*.conf;
  40. location / {
  41. root /root/www/;
  42. index index.html index.htm;
  43. }
  44. location /chat3/ {
  45. proxy_pass https://你的外网ip:3003/;
  46. proxy_redirect off;
  47. proxy_http_version 1.1;
  48. proxy_set_header Upgrade $http_upgrade;
  49. proxy_set_header Connection "upgrade";
  50. }
  51. location /chat/ {
  52. proxy_pass https://你的外网ip:5000/;
  53. proxy_redirect off;
  54. proxy_http_version 1.1;
  55. proxy_set_header Upgrade $http_upgrade;
  56. proxy_set_header Connection "upgrade";
  57. }
  58. location /mes/ {
  59. proxy_pass http://你的外网ip:3011/;
  60. proxy_redirect off;
  61. proxy_set_header Host $host;
  62. proxy_set_header X-Real-IP $remote_addr;
  63. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  64. }
  65. location /upload/ {
  66. proxy_pass http://172.16.0.4:8083/;
  67. proxy_redirect off;
  68. proxy_set_header Host $host;
  69. proxy_set_header X-Real-IP $remote_addr;
  70. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  71. }
  72. error_page 404 /404.html;
  73. location = /40x.html {
  74. }
  75. error_page 500 502 503 504 /50x.html;
  76. location = /50x.html {
  77. }
  78. }

以上是nginx.conf的配置。一般配置https都会这样配置,但是如果你想http也可以访问那么加上以下代码

  1. listen 80 default_server;
  2. listen [::]:80 default_server;

这里注意的是,需要注释

  1. #ssl on;

这样才能两者都可以访问。

还有在配置nginx.conf,需要注意

  1. location /chat/ {
  2. proxy_pass https://你的外网ip:5000/;
  3. proxy_redirect off;
  4. proxy_http_version 1.1;
  5. proxy_set_header Upgrade $http_upgrade;
  6. proxy_set_header Connection "upgrade";
  7. }

这处至关重要,我用了3天才解决https访问问题,就是因为这处。所以当你使用node的https服务的时候,不妨根据我上面那段代码。proxy_pass https://你的外网ip:5000/;这里是关键。

然后呢,我使用的是node服务端,因为要使用https访问,所以要引入https服务。
以下是我的项目实例:
nodejs

  1. var https = require('https');
  2. var fs=require("fs");
  3. var express = require('express');
  4. var ws=require("socket.io");
  5. var path=require("path");
  6. var _ = require('underscore');
  7. var usocket = [];
  8. var usocket1 = [];
  9. var pass=[];
  10. var data=[];
  11. var hashName = { };
  12. var onlineCount = 0;
  13. var app = express();
  14. app.all("*", function (req, res, next) {
  15. //设置允许跨域的域名,*代表允许任意域名跨域
  16. res.header("Access-Control-Allow-Origin", "*");
  17. //允许的header类型
  18. res.header("Access-Control-Allow-Headers", "content-type");
  19. //跨域允许的请求方式
  20. res.header("Access-Control-Allow-Methods", "DELETE,PUT,POST,GET,OPTIONS");
  21. if (req.method.toLowerCase() == 'options')
  22. res.send(200); //让options尝试请求快速结束
  23. else
  24. next();
  25. })
  26. //你的证书
  27. var options = {
  28. key: fs.readFileSync('./2_www.maomin.club.key'),
  29. cert: fs.readFileSync('./1_www.maomin.club_bundle.crt')
  30. }
  31. // 静态文件识别
  32. app.use(express.static('./public'));
  33. var server = https.createServer(options,app)
  34. function get_file_content(filepath) {
  35. return fs.readFileSync(filepath);
  36. }
  37. // 获取在线
  38. function broadcast() {
  39. io.sockets.emit("dataval", hashName);
  40. }
  41. // 提供私有socket
  42. function privateSocket(toId) {
  43. return (_.findWhere(io.sockets.sockets, {
  44. id: toId
  45. }));
  46. }
  47. // 封装删除
  48. function removeByValue(arr, val) {
  49. for (var i = 0; i < arr.length; i++) {
  50. if (arr[i] == val) {
  51. arr.splice(i, 1);
  52. break;
  53. }
  54. }
  55. }
  56. // 连接socket
  57. var io=ws(server);
  58. io.on("connection",function(socket){
  59. // 写入成功后读取测试
  60. fs.readFile('./user.xls', 'utf-8', function (err, data) {
  61. if(data!=null){
  62. var value = data.split('\n');
  63. io.sockets.emit("users", value);
  64. }
  65. });
  66. // 写入成功后读取测试
  67. fs.readFile('./password.xls', 'utf-8', function (err,data) {
  68. if(data!=null){
  69. var pass1=data.split('\n');
  70. io.sockets.emit("pass", pass1);
  71. }
  72. });
  73. broadcast();
  74. // 生成名字
  75. socket.on('setName', function (data) {
  76. var name = data;
  77. hashName[name] = socket.id;
  78. // console.log(hashName[name]);
  79. broadcast();
  80. });
  81. // 私聊发送
  82. socket.on('sayTo', function (data) {
  83. var toName = data.to;
  84. var toId;
  85. console.log(toName);
  86. if (toId = hashName[toName]) {
  87. privateSocket(toId).emit('message1', data);
  88. }
  89. });
  90. // 群发
  91. socket.on("message",function(mes){
  92. // console.log(mes);
  93. io.emit("message", mes);
  94. });
  95. // 离开
  96. socket.on('disconnect', function (name) {
  97. io.emit('disconnected', --onlineCount);
  98. name=this.i2;
  99. io.emit("disconnect", name);
  100. removeByValue(data, name);
  101. io.sockets.emit("dataval", data);
  102. })
  103. // 在线
  104. socket.on('time', function (val) {
  105. // console.log(val);
  106. })
  107. // 注册
  108. socket.on("reg", function (name) {
  109. usocket[name] = socket;
  110. this.i1=name;
  111. io.emit("reg", name);
  112. var myname =this.i1+"\n";
  113. fs.writeFile('./user.xls', myname, {
  114. 'flag': 'a'
  115. }, function (err) {
  116. if (err) {
  117. throw err;
  118. }
  119. // 写入成功后读取测试
  120. fs.readFile('./user.xls', 'utf-8', function (err,data) {
  121. if (err) {
  122. throw err;
  123. }
  124. });
  125. });
  126. })
  127. // 加入
  128. io.emit('connected', ++onlineCount);
  129. // console.log(data);
  130. io.sockets.emit("array", data);
  131. socket.on("join", function (name) {
  132. usocket1[name] = socket;
  133. this.i2 = name;
  134. io.emit("join", name);
  135. data.push(name);
  136. io.sockets.emit("dataval", data);
  137. })
  138. // 密码
  139. socket.on("pass",function(val){
  140. pass[val]=socket;
  141. this.i2=val;
  142. io.emit("pass", val);
  143. var password=this.i2+"\n";
  144. fs.writeFile('./password.xls', password, {
  145. 'flag': 'a'
  146. }, function (err) {
  147. if (err) {
  148. throw err;
  149. }
  150. });
  151. })
  152. });
  153. server.listen(5000)
  154. console.log("服务器运行中");

最后呢,你需要在前台js中修改如下:
index.js

  1. var socket = io.connect("https://域名:5000/");

这样你就可以使用https访问了。
在这里插入图片描述

发表评论

表情:
评论列表 (有 0 条评论,373人围观)

还没有评论,来说两句吧...

相关阅读

    相关 看到技术债务

    “软件和大教堂类似,都是先构建,然后祈祷”。————Earl Everett 关于技术债务的讨论时而蔓延时而消退,技术债务仿佛是个筐,什么东西都可以往里装,然而当我们

    相关 项目技术

    首先很感谢公司给我时间弥补知识上的不足,这段时间对整个项目有了大概一个理解   整个项目基于vue-cli、webpack、nodejs构建的目录结构   整个项目流程