服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

云服务器|WEB服务器|FTP服务器|邮件服务器|虚拟主机|服务器安全|DNS服务器|服务器知识|Nginx|IIS|Tomcat|

服务器之家 - 服务器技术 - Nginx - nginx通过https部署vue项目的完整步骤

nginx通过https部署vue项目的完整步骤

2022-08-09 10:14我犟不过你 Nginx

在实际开发中,我们会以https形式进行页面访问,下面这篇文章主要给大家介绍了关于nginx通过https部署vue项目的完整步骤,文中通过示例代码介绍的非常详细,需要的朋友可以参考下

本篇主要记录vue项目,通过nginx实现https部署的免费方案。主要参考步骤和关键点如下所示。

一、生成证书

进入nginx安装目录

?
1
2
3
4
5
# 进入nginx目录
[root@hecs-402944 nginx]# cd /etc/nginx/
[root@hecs-402944 nginx]# ls
conf.d     fastcgi.conf          fastcgi_params          koi-utf  mime.types          nginx.conf          scgi_params          uwsgi_params          win-utf
default.d  fastcgi.conf.default  fastcgi_params.default  koi-win  mime.types.default  nginx.conf.default  scgi_params.default  uwsgi_params.default

创建ssl文件目录

?
1
2
3
# 创建ssl目录
[root@hecs-402944 nginx]# mkdir ssl
[root@hecs-402944 nginx]# cd ssl

生成server.key,需要设置两次密码

?
1
2
3
4
5
6
7
[root@hecs-402944 ssl]# openssl genrsa -aes256 -out server.key 2048
Generating RSA private key, 2048 bit long modulus
...........................................................+++
................................................................................................+++
e is 65537 (0x10001)
Enter pass phrase for server.key:
Verifying - Enter pass phrase for server.key:

生成无密码的server.key

?
1
2
3
4
5
6
# 生成无密码的server.key
[root@hecs-402944 ssl]# openssl rsa -in server.key -out server.key
Enter pass phrase for server.key:
writing RSA key
[root@hecs-402944 ssl]# ls
server.key

创建服务器证书的申请文件 server.csr

此处需要填写具体内容,国家、省份、城市、公司、行业、网站、邮箱等,后面的两次密码直接回车就好。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
[root@hecs-402944 ssl]# openssl req -new -key server.key -out server.csr
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
## 国家
Country Name (2 letter code) [XX]:CN
## 省份
State or Province Name (full name) []:heilongjiang
## 城市
Locality Name (eg, city) [Default City]:haerbin
## 公司
Organization Name (eg, company) [Default Company Ltd]:xxxxxxx
## 行业
Organizational Unit Name (eg, section) []:IT
## 网站
Common Name (eg, your name or your server's hostname) []:www.xxxxxxx.com
## 邮箱
Email Address []:xxxxxx@xxxxxx.com.cn
 
Please enter the following 'extra' attributes
to be sent with your certificate request
## 两次回车
A challenge password []:
An optional company name []:
[root@hecs-402944 ssl]# ll
总用量 8
-rw-r--r-- 1 root root 1082 5月  13 09:15 server.csr
-rw-r--r-- 1 root root 1679 5月  13 09:11 server.key

生成crt证书文件server.crt

?
1
2
3
4
5
# 生成crt证书文件server.crt
[root@hecs-402944 ssl]# openssl x509 -in server.csr -out server.crt -req -signkey server.key -days 3650
Signature ok
subject=/C=CN/ST=heilongjiang/L=haerbin/O=dongfangtongwangxin/OU=IT/CN=www.tongtech.com/emailAddress=weirx@mvtech.com.cn
Getting Private key

二、nginx配置

前端项目使用nginx部署,所以我们采用nginx爆率443端口,开启ssl。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
server {
       listen 443 ssl http2 default_server;
       listen [::]:443 ssl http2 default_server;
 
       ssl on;
       ssl_certificate "/etc/nginx/ssl/server.crt";
       ssl_certificate_key "/etc/nginx/ssl/server.key";
       ssl_session_cache shared:SSL:1m;
       ssl_session_timeout 10m;
       ssl_ciphers HIGH:!aNULL:!MD5;
       ssl_prefer_server_ciphers on;
       
        server_name  _;
        
        include /etc/nginx/default.d/*.conf;
 
        root /opt/vue/dist;
 
        gzip_static on;
 
    location / {
         proxy_pass http://localhost:13000;
        }
 
           # 支持websocket的配置项
        location /websocket {
            proxy_pass http://localhost:13000;
            # WebScoket Support
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
        }
        }

三、修改vue配置文件

项目使用qiankun框架,分为主工程和子工程,所以我们需要修改在主工程当中的子工程配置,主要是访问的ip地址。

修改.env文件,在使用http时是需要配置ip+端口的,修改为https直接使用ip即可。

?
1
2
3
4
5
6
7
#开发环境env配置
NODE_ENV=production
VUE_APP_SYSTEM_URL=//172.16.3.30/system
VUE_APP_COMPONENTS_URL=//172.16.3.30/components
VUE_APP_API_BASIC_URL=//172.16.3.30/basic
 
VUE_APP_SOCKETURL = 'wss://172.16.3.29'

如上所示,需要将websocket的请求地址由ws修改为wss,否则会报错。

总结

到此这篇关于nginx通过https部署vue项目的文章就介绍到这了,更多相关nginx通过https部署vue内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://juejin.cn/post/7098143397071814663

延伸 · 阅读

精彩推荐
  • NginxNginx部署https网站并配置地址重写的步骤详解

    Nginx部署https网站并配置地址重写的步骤详解

    今天小编就为大家分享一篇关于Nginx部署https网站并配置地址重写的步骤详解,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友...

    wdz306ling4352020-01-05
  • Nginx利用Nginx反向代理解决跨域问题详解

    利用Nginx反向代理解决跨域问题详解

    这篇文章主要介绍了利用Nginx反向代理解决跨域问题详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧 ...

    suan_suan2362020-01-03
  • Nginxnginx禁止某个IP访问站点的设置方法

    nginx禁止某个IP访问站点的设置方法

    近期发现博客遭到某些人的恶意灌水,频繁地利用发帖机器人发表评论,给博客的管理带来诸多不便,搜索了一下资料,可以利用nginx的ngx_http_access_module...

    nginx技术网5612019-10-10
  • Nginx利用nginx + fastcgi实现图片识别服务器

    利用nginx + fastcgi实现图片识别服务器

    这篇文章主要给大家介绍了关于如何利用nginx + fastcgi实现图片识别服务器的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定...

    鱼香土豆丝5432020-01-04
  • Nginx详解常用的nginx rewrite重写规则

    详解常用的nginx rewrite重写规则

    这篇文章主要介绍了详解常用的nginx rewrite重写规则,Nginx的rewrite功能是使用nginx提供的全局变量或自己设置的变量,结合正则表达式和标志位实现url重写以...

    LanceZhen4242020-01-05
  • Nginx详解阿里云nginx服务器多站点的配置

    详解阿里云nginx服务器多站点的配置

    本篇文章主要介绍了阿里云nginx服务器多站点的配置,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧 ...

    xxw19966162019-11-18
  • NginxNginx配置文件详解

    Nginx配置文件详解

    Nginx是lgor Sysoev为俄罗斯访问量第二的rambler.ru站点设计开发的。Nginx功能丰富,可作为HTTP服务器,也可作为反向代理服务器,邮件服务器。这篇文章主要介...

    张龙豪5302019-11-27
  • Nginx详解nginx日志定时备份和删除

    详解nginx日志定时备份和删除

    本篇文章主要介绍了nginx日志定时备份和删除 ,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧 ...

    自强5962019-11-29