https & nginx

 

 

  • npm install http-server -g , 用  http-server 来启动静态文件, cd dist -> http-server -p 8081 就可以了, 注意 dist 里面需要 index.html
  • 生成证书:openssl req -newkey rsa:2048 -new -nodes -x509 -days 3650 -keyout key.pem -out cert.pem
    • 当前目录下生成两个 一个是cert.pem, 一个是key.pem 
  • http-server -S -C cert.pem
    •  -C 后面跟cert的路径,-K 后面跟key的路径,注意填绝对路径
    • 不写 -C 或者 -K 的时候的默认值:-C 默认是 cert.pem、-K 默认是 key.pem
    • -S 启用 SSL/TLS(即 HTTPS)
  • 这样浏览器会提示不安全,因为浏览器不信任这个证书

  

  • 未测试,需要域名
  • 安装 Certbot 工具:sudo apt install certbot
  • 获取证书:certbot certonly --standalone -d yourdomain.com  // 这里的 yourdomain.com 需要是一个域名 
  • 配置 HTTPS 服务器以使用生成的证书。

 

  • nginx 比 http-server 要更强大:apt install nginx  (1.24.0) 
  • 重启:systemctl restart nginx  ; windows上面:开启:./nginx.exe &  、关闭:./nginx.exe -s stop、重启:./nginx.exe -s reload
  • 下载之后默认是打开了一个服务器,端口是 80 ,是在 /etc/nginx/sites-available/default 里面开启的 (因为  /etc/nginx/nginx.conf 里 include /etc/nginx/sites-enabled/*;  你也可以在 /etc/nginx/nginx.conf  里把默认的 80 端口 服务给关掉) 。 然后我是在 /etc/nginx/nginx.conf 里添加自己的服务,如下:
    • nginx.conf
       user www-data; # 指定运行 Nginx 的用户和组
      worker_processes auto; #根据 CPU 核心数自动设置工作进程数。
      pid /run/nginx.pid; # 指定 Nginx 主进程的 PID 文件路径
      error_log /var/log/nginx/error.log;
      include /etc/nginx/modules-enabled/*.conf;
      
      events {
      	worker_connections 768;
      	# multi_accept on;
      }
      
      http {
      
      	##
      	# Basic Settings
      	##
      
      	sendfile on; # 启用高效文件传输模式。
      	tcp_nopush on; # 防止数据包分片
      	types_hash_max_size 2048;
      	# server_tokens off;
      
      	# server_names_hash_bucket_size 64;
      	# server_name_in_redirect off;
      
      	include /etc/nginx/mime.types;
      	default_type application/octet-stream;
      
      	##
      	# SSL Settings
      	##
      
      	ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; # Dropping SSLv3, ref: POODLE
      	ssl_prefer_server_ciphers on;
      
      	##
      	# Logging Settings
      	##
      
      	access_log /var/log/nginx/access.log;
      
      	##
      	# Gzip Settings
      	##
      
      	gzip on;
      
      	# gzip_vary on;
      	# gzip_proxied any;
      	# gzip_comp_level 6;
      	# gzip_buffers 16 8k;
      	# gzip_http_version 1.1;
      	# gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
      
      	##
      	# Virtual Host Configs
      	##
      
      	include /etc/nginx/conf.d/*.conf;
      	include /etc/nginx/sites-enabled/*; # 里面加载了默认的 80 端口服务
      
          # fxw add
          server {
              listen 8081;  # 监听 HTTP 的 80 端口
              # server_name localhost;  # 域名或 IP 地址
      
              root /root/vue/2-测试; # 前端文件路径
          }
      
          # fxw add 可以开启多个
          # server {
          #     listen 8080;  # 监听 HTTP 的 80 端口
          #     # server_name localhost;  # 域名或 IP 地址
          #     root /root/vue/2-测试; # 前端文件路径
          # }
      
      }
      
      
      #mail {
      #	# See sample authentication script at:
      #	# http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
      #
      #	# auth_http localhost/auth.php;
      #	# pop3_capabilities "TOP" "USER";
      #	# imap_capabilities "IMAP4rev1" "UIDPLUS";
      #
      #	server {
      #		listen     localhost:110;
      #		protocol   pop3;
      #		proxy      on;
      #	}
      #
      #	server {
      #		listen     localhost:143;
      #		protocol   imap;
      #		proxy      on;
      #	}
      #}

       

       

posted @ 2025-02-17 16:36  封兴旺  阅读(13)  评论(0)    收藏  举报

联系方式: 18274305123(微信同号)