当前位置: 首页 > news >正文

前端vue部署到nginx并且配置https安全证书全流程

        说明一下: 本人原本使用的是docker安装nginx通过挂载实现部署,但是出现了很多bug(例如部署安全证书后还是无法访问),所以困扰了很久,最后改为本地安装nginx,最终在不懈的努力下终于按照好了,特此记录一下。

        一:整个流程:

              1. 将前端项目打包,会生成dist文件(同时不要忘了修改调用后台的ip)

              2. 安装nginx(本地安装,非docker),然后将dist下的文件放入nginx的html目录下

              3. 配置nginx的配置文件

              4. 安装证书(ssl)

            安装nginx,ssl参考:https://blog.csdn.net/oYingJie1/article/details/127700897

            下载及安装ssl参考:https://blog.csdn.net/qq_42320934/article/details/120655799

        二: 附上关键代码及说明

                1.nginx的配置文件

#user  nobody;
worker_processes  1;#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;#pid        logs/nginx.pid;events {worker_connections  1024;
}http {include       mime.types;default_type  application/octet-stream;#log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '#                  '$status $body_bytes_sent "$http_referer" '#                  '"$http_user_agent" "$http_x_forwarded_for"';#access_log  logs/access.log  main;sendfile        on;#tcp_nopush     on;#keepalive_timeout  0;keepalive_timeout  65;#gzip  on;server {listen       80;server_name  localhost;#将所有HTTP请求通过rewrite指令重定向到HTTPS。rewrite ^(.*)$ https://$host$1;#charset koi8-r;#access_log  logs/host.access.log  main;location / {root   html;index  index.html index.htm;}location /undefined/  {proxy_pass http://s11.s100.vip:35053;proxy_redirect default;rewrite ^/undefined/(.*) /$1 break;}#error_page  404              /404.html;# redirect server error pages to the static page /50x.html#error_page   500 502 503 504  /50x.html;location = /50x.html {root   html;}# proxy the PHP scripts to Apache listening on 127.0.0.1:80##location ~ \.php$ {#    proxy_pass   http://127.0.0.1;#}# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000##location ~ \.php$ {#    root           html;#    fastcgi_pass   127.0.0.1:9000;#    fastcgi_index  index.php;#    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;#    include        fastcgi_params;#}# deny access to .htaccess files, if Apache's document root# concurs with nginx's one##location ~ /\.ht {#    deny  all;#}}# another virtual host using mix of IP-, name-, and port-based configuration##server {#    listen       8000;#    listen       somename:8080;#    server_name  somename  alias  another.alias;#    location / {#        root   html;#        index  index.html index.htm;#    }#}# HTTPS server#server {listen       443 ssl;server_name  localhost;ssl_certificate      cert.pem;ssl_certificate_key  cert.key;ssl_session_cache    shared:SSL:1m;ssl_session_timeout  5m;ssl_ciphers  HIGH:!aNULL:!MD5;ssl_prefer_server_ciphers  on;location / {root   html;index  index.html index.htm;}location /undefined/  {proxy_pass http://s11.s100.vip:35053;proxy_redirect default;rewrite ^/undefined/(.*) /$1 break;}}}
        2. 以前docker配置的文件

user  nginx;
worker_processes  auto;error_log  /var/log/nginx/error.log notice;
pid        /var/run/nginx.pid;events {worker_connections  1024;
}http {include       /etc/nginx/mime.types;default_type  application/octet-stream;log_format  main  '$remote_addr - $remote_user [$time_local] "$request" ''$status $body_bytes_sent "$http_referer" ''"$http_user_agent" "$http_x_forwarded_for"';access_log  /var/log/nginx/access.log  main;sendfile        on;#tcp_nopush     on;keepalive_timeout  65;#gzip  on;include /etc/nginx/conf.d/*.conf;server {listen       80;listen  [::]:80;server_name  www.slgstu.top;#将所有HTTP请求通过rewrite指令重定向到HTTPS。
#    rewrite ^(.*)$ https://$host$1;#access_log  /var/log/nginx/host.access.log  main;location / {root   /usr/share/nginx/html;index  index.html index.htm;}location /undefined/  {proxy_pass http://s11.s100.vip:35053;proxy_redirect default;rewrite ^/undefined/(.*) /$1 break;}#error_page  404              /404.html;# redirect server error pages to the static page /50x.html#error_page   500 502 503 504  /50x.html;location = /50x.html {root   /usr/share/nginx/html;}# proxy the PHP scripts to Apache listening on 127.0.0.1:80##location ~ \.php$ {#    proxy_pass   http://127.0.0.1;#}# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000##location ~ \.php$ {#    root           html;#    fastcgi_pass   127.0.0.1:9000;#    fastcgi_index  index.php;#    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;#    include        fastcgi_params;#}# deny access to .htaccess files, if Apache's document root# concurs with nginx's one##location ~ /\.ht {#    deny  all;#}
}server {#HTTPS的默认访问端口443。#如果未在此处配置HTTPS的默认访问端口,可能会造成Nginx无法启动。listen 443 ssl;#填写证书绑定的域名server_name www.slgstu.top;#填写证书文件名称ssl_certificate cert/www.slgstu.top.cer;#填写证书私钥文件名称ssl_certificate_key cert/www.slgstu.top.key;ssl_session_cache    shared:SSL:1m;ssl_session_timeout  5m;# 指定密码为openssl支持的格式ssl_protocols  SSLv2 SSLv3 TLSv1.2;ssl_ciphers  HIGH:!aNULL:!MD5;  # 密码加密方式ssl_prefer_server_ciphers  on;   # 依赖SSLv3和TLSv1协议的服务器密码将优先于客户端密码location / {root   /usr/share/nginx/html;index  index.html index.htm;}
}}
                3.说明下nginx的文件作用

                        cert:放https证书的两个文件

                        conf: nginx的一些配置文件,主要还是使用nginx.conf

                        html:默认的话nginx会加载html文件下的index.html

                        log:查看成功与错误日志

http://www.lryc.cn/news/95836.html

相关文章:

  • 三子棋(超详解+完整码源)
  • 【算法提高:动态规划】1.2 最长上升子序列模型(TODO:最长公共上升子序列)
  • 会不会好奇ai绘画生成器?ai创作的灵感从何而来?
  • 【Ajax】笔记-JQuery发送请求与通用方法
  • 视频的音频提取怎么做?这样提取很简单
  • 几百本常用计算机开发语言电子书链接
  • Docker Compose 解析:定义和管理多容器应用,从多角度探索其优势和应用场景
  • Linux系列---【CentOS 7通过MSTSC连接远程桌面】
  • width: calc(~“100% - 267px“);动态css 调样式
  • Windows Server 2012 搭建网关服务器并端口转发
  • 基于linux下的高并发服务器开发(第三章)- 3.10 死锁
  • 09.计算机网络——套接字编程
  • Data Structure, Algorithm,and Applications in C++
  • Apipost使用教程
  • 如何使用Python进行服务器管理和自动化操作?
  • Kafka-partition和消费者的关系
  • 使用克拉默法则进行三点定圆(二维)
  • 【Java】Java多线程编程基础
  • FFmpeg-4.2.4的去logo源码分析
  • 深度学习(一)
  • Stream API将对象中的某一字段取出转换为list或数组
  • 什么是Java中的JVM(Java虚拟机)?
  • springboot + redis + 注解 + 拦截器 实现接口幂等性校验
  • PLC编程:关键在于模拟操作流程和实现控制
  • List的各种排序
  • 在自定义数据集上微调Alpaca和LLaMA
  • Python 实现接口类的两种方式+邮件提醒+动态导入模块+反射(参考Django中间件源码)
  • Solr原理剖析
  • 解决 “无法将 ‘npm‘ 项识别为 cmdlet、函数、脚本文件或可运行程序的名称“ 错误的方法
  • Python 电商API 开发最佳实践