您现在的位置是:首页 > 技术教程 正文

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

admin 阅读: 2024-04-01
后台-插件-广告管理-内容页头部广告(手机)
        说明一下: 本人原本使用的是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的配置文件
  1. #user nobody;
  2. worker_processes 1;
  3. #error_log logs/error.log;
  4. #error_log logs/error.log notice;
  5. #error_log logs/error.log info;
  6. #pid logs/nginx.pid;
  7. events {
  8. worker_connections 1024;
  9. }
  10. http {
  11. include mime.types;
  12. default_type application/octet-stream;
  13. #log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  14. # '$status $body_bytes_sent "$http_referer" '
  15. # '"$http_user_agent" "$http_x_forwarded_for"';
  16. #access_log logs/access.log main;
  17. sendfile on;
  18. #tcp_nopush on;
  19. #keepalive_timeout 0;
  20. keepalive_timeout 65;
  21. #gzip on;
  22. server {
  23. listen 80;
  24. server_name localhost;
  25. #将所有HTTP请求通过rewrite指令重定向到HTTPS。
  26. rewrite ^(.*)$ https://$host$1;
  27. #charset koi8-r;
  28. #access_log logs/host.access.log main;
  29. location / {
  30. root html;
  31. index index.html index.htm;
  32. }
  33. location /undefined/ {
  34. proxy_pass http://s11.s100.vip:35053;
  35. proxy_redirect default;
  36. rewrite ^/undefined/(.*) /$1 break;
  37. }
  38. #error_page 404 /404.html;
  39. # redirect server error pages to the static page /50x.html
  40. #
  41. error_page 500 502 503 504 /50x.html;
  42. location = /50x.html {
  43. root html;
  44. }
  45. # proxy the PHP scripts to Apache listening on 127.0.0.1:80
  46. #
  47. #location ~ \.php$ {
  48. # proxy_pass http://127.0.0.1;
  49. #}
  50. # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  51. #
  52. #location ~ \.php$ {
  53. # root html;
  54. # fastcgi_pass 127.0.0.1:9000;
  55. # fastcgi_index index.php;
  56. # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
  57. # include fastcgi_params;
  58. #}
  59. # deny access to .htaccess files, if Apache's document root
  60. # concurs with nginx's one
  61. #
  62. #location ~ /\.ht {
  63. # deny all;
  64. #}
  65. }
  66. # another virtual host using mix of IP-, name-, and port-based configuration
  67. #
  68. #server {
  69. # listen 8000;
  70. # listen somename:8080;
  71. # server_name somename alias another.alias;
  72. # location / {
  73. # root html;
  74. # index index.html index.htm;
  75. # }
  76. #}
  77. # HTTPS server
  78. #
  79. server {
  80. listen 443 ssl;
  81. server_name localhost;
  82. ssl_certificate cert.pem;
  83. ssl_certificate_key cert.key;
  84. ssl_session_cache shared:SSL:1m;
  85. ssl_session_timeout 5m;
  86. ssl_ciphers HIGH:!aNULL:!MD5;
  87. ssl_prefer_server_ciphers on;
  88. location / {
  89. root html;
  90. index index.html index.htm;
  91. }
  92. location /undefined/ {
  93. proxy_pass http://s11.s100.vip:35053;
  94. proxy_redirect default;
  95. rewrite ^/undefined/(.*) /$1 break;
  96. }
  97. }
  98. }
        2. 以前docker配置的文件
  1. user nginx;
  2. worker_processes auto;
  3. error_log /var/log/nginx/error.log notice;
  4. pid /var/run/nginx.pid;
  5. events {
  6. worker_connections 1024;
  7. }
  8. http {
  9. include /etc/nginx/mime.types;
  10. default_type application/octet-stream;
  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. keepalive_timeout 65;
  18. #gzip on;
  19. include /etc/nginx/conf.d/*.conf;
  20. server {
  21. listen 80;
  22. listen [::]:80;
  23. server_name www.slgstu.top;
  24. #将所有HTTP请求通过rewrite指令重定向到HTTPS。
  25. # rewrite ^(.*)$ https://$host$1;
  26. #access_log /var/log/nginx/host.access.log main;
  27. location / {
  28. root /usr/share/nginx/html;
  29. index index.html index.htm;
  30. }
  31. location /undefined/ {
  32. proxy_pass http://s11.s100.vip:35053;
  33. proxy_redirect default;
  34. rewrite ^/undefined/(.*) /$1 break;
  35. }
  36. #error_page 404 /404.html;
  37. # redirect server error pages to the static page /50x.html
  38. #
  39. error_page 500 502 503 504 /50x.html;
  40. location = /50x.html {
  41. root /usr/share/nginx/html;
  42. }
  43. # proxy the PHP scripts to Apache listening on 127.0.0.1:80
  44. #
  45. #location ~ \.php$ {
  46. # proxy_pass http://127.0.0.1;
  47. #}
  48. # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  49. #
  50. #location ~ \.php$ {
  51. # root html;
  52. # fastcgi_pass 127.0.0.1:9000;
  53. # fastcgi_index index.php;
  54. # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
  55. # include fastcgi_params;
  56. #}
  57. # deny access to .htaccess files, if Apache's document root
  58. # concurs with nginx's one
  59. #
  60. #location ~ /\.ht {
  61. # deny all;
  62. #}
  63. }
  64. server {
  65. #HTTPS的默认访问端口443
  66. #如果未在此处配置HTTPS的默认访问端口,可能会造成Nginx无法启动。
  67. listen 443 ssl;
  68. #填写证书绑定的域名
  69. server_name www.slgstu.top;
  70. #填写证书文件名称
  71. ssl_certificate cert/www.slgstu.top.cer;
  72. #填写证书私钥文件名称
  73. ssl_certificate_key cert/www.slgstu.top.key;
  74. ssl_session_cache shared:SSL:1m;
  75. ssl_session_timeout 5m;
  76. # 指定密码为openssl支持的格式
  77. ssl_protocols SSLv2 SSLv3 TLSv1.2;
  78. ssl_ciphers HIGH:!aNULL:!MD5; # 密码加密方式
  79. ssl_prefer_server_ciphers on; # 依赖SSLv3和TLSv1协议的服务器密码将优先于客户端密码
  80. location / {
  81. root /usr/share/nginx/html;
  82. index index.html index.htm;
  83. }
  84. }
  85. }
                3.说明下nginx的文件作用

                        cert:放https证书的两个文件

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

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

                        log:查看成功与错误日志

标签:
声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

在线投稿:投稿 站长QQ:1888636

后台-插件-广告管理-内容页尾部广告(手机)
关注我们

扫一扫关注我们,了解最新精彩内容

搜索