eggjs+vue+nginx配置
- 2020 年 3 月 18 日
- 筆記
安裝node
https://github.com/nodesource/distributions#installation-instructions-1
注意使用No root privileges方法安裝
安裝yarn
curl --silent --location https://dl.yarnpkg.com/rpm/yarn.repo | sudo tee /etc/yum.repos.d/yarn.repo sudo yum install yarn
安裝nginx
//安裝nginx的命令 sudo yum install epel-release sudo yum install nginx //讓nginx隨系統啟動而啟動 sudo systemctl enable nginx //常用命令 //啟動:nginx //停止:nginx -s stop //重載配置:nginx -s reload //配置路徑:/etc/nginx/nginx.conf //日誌路徑:/var/log/nginx
nginx關鍵配置
server { listen 80 default_server; listen [::]:80 default_server; server_name domain.com www.domain.com; root /home/client/; location / { try_files $uri $uri/ /index.html; } location /api/ { proxy_pass http://127.0.0.1:7001/; } }注意兩個location和root,不要把vue的靜態文件放在root目錄下
注意最後一個location中,api和7001後面都有斜杠
nginx證書配置
server { listen 443; server_name your-domain.com www.your-domain.com; ssl on; ssl_certificate /root/ssl/your-domain.crt; ssl_certificate_key /root/ssl/your-domain.key; ssl_session_timeout 5m; ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4; ssl_protocols TLSv1 TLSv1.1 TLSv1.2; ssl_prefer_server_ciphers on; location / { proxy_buffer_size 128k; proxy_buffers 32 32k; proxy_busy_buffers_size 128k; proxy_pass http://127.0.0.1:912; } } server { listen 80; server_name your-domain.com www.your-domain.com; rewrite ^(.*)$ https://$host$1 permanent; }為了調試方便,vue.config.js的配置如下
module.exports = { devServer: { proxy: { '/api': { target:'http://127.0.0.1:7001', changeOrigin: true, secure: false, pathRewrite: { '^/api': '/' //重寫之後url為 http://127.0.0.1:7001/xxxx //pathRewrite: {'^/api': '/api'} 重寫之後url為 http://192.168.1.16:8085/api/xxxx } } } } };egg.js調試配置如下:config文件夾下plugin.js
module.exports = { cors: { enable: true, package: 'egg-cors' } };config.default.js:注意這裡的配置是有些危險的,因為可能會同步到伺服器上去
config.security = { csrf: { enable: false }, domainWhiteList: [ '*' ] }; config.cors = { origin: '*', allowMethods: 'GET,HEAD,PUT,POST,DELETE,PATCH,OPTIONS' };

