nginx通過https部署vue項(xiàng)目的完整步驟
本篇主要記錄vue項(xiàng)目,通過nginx實(shí)現(xiàn)https部署的免費(fèi)方案。主要參考步驟和關(guān)鍵點(diǎn)如下所示。
一、生成證書
進(jìn)入nginx安裝目錄
# 進(jìn)入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
創(chuàng)建ssl文件目錄
# 創(chuàng)建ssl目錄 [root@hecs-402944 nginx]# mkdir ssl [root@hecs-402944 nginx]# cd ssl
生成server.key,需要設(shè)置兩次密碼
[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
# 生成無密碼的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
創(chuàng)建服務(wù)器證書的申請(qǐng)文件 server.csr
此處需要填寫具體內(nèi)容,國(guó)家、省份、城市、公司、行業(yè)、網(wǎng)站、郵箱等,后面的兩次密碼直接回車就好。
[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. ----- ## 國(guó)家 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 ## 行業(yè) Organizational Unit Name (eg, section) []:IT ## 網(wǎng)站 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
# 生成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配置
前端項(xiàng)目使用nginx部署,所以我們采用nginx爆率443端口,開啟ssl。
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的配置項(xiàng)
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配置文件
項(xiàng)目使用qiankun框架,分為主工程和子工程,所以我們需要修改在主工程當(dāng)中的子工程配置,主要是訪問的ip地址。
修改.env文件,在使用http時(shí)是需要配置ip+端口的,修改為https直接使用ip即可。
#開發(fā)環(huán)境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的請(qǐng)求地址由ws修改為wss,否則會(huì)報(bào)錯(cuò)。
總結(jié)
到此這篇關(guān)于nginx通過https部署vue項(xiàng)目的文章就介紹到這了,更多相關(guān)nginx通過https部署vue內(nèi)容請(qǐng)搜索本站以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持本站!
版權(quán)聲明:本站文章來源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請(qǐng)保持原文完整并注明來源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非maisonbaluchon.cn所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學(xué)習(xí)參考,不代表本站立場(chǎng),如有內(nèi)容涉嫌侵權(quán),請(qǐng)聯(lián)系alex-e#qq.com處理。
關(guān)注官方微信