vue.js前端專案部署到nginx伺服器
注:
- 本文選擇了nginx做web伺服器。
- 因為在本文vue前端專案中,需要從介面獲取資料,可是資料與vue前端專案是在同一ip地址但不同埠號,所以需要跨域讀取資料。本文的跨域處理是使用了nginx。
環境:
- 阿里雲伺服器(ubuntu 64bit)
- xshell
- nginx
1.使用xshell登入到阿里雲伺服器。安裝nginx(本文安裝到/etc下)
[plain] view plain copy print?- cd /etc
- apt-get update
- apt-get install nginx
cd /etc
apt-get update
apt-get install nginx
2.首先先配置nginx,然後再根據配置檔案做下一步操作
開啟/etc/nginx/nginx.conf檔案
[plain] view plain copy print?- vim /etc/nginx/nginx.conf
vim /etc/nginx/nginx.conf
在nginx.conf中配置如下:
[plain] view plain copy print?- user www-data;
- worker_processes auto;
- pid /run/nginx.pid;
- events {
- worker_connections 768;
- # multi_accept on;
- }
- http {
- ##
- # Basic Settings
- ##
- tcp_nodelay on;
- keepalive_timeout 65;
- types_hash_max_size 2048;
- # server_tokens off;
- # server_names_hash_bucket_size 64;
- # server_name_in_redirect off;
- include /etc/nginx/mime.types;
- default_type application/octet-stream;
- ##
- # SSL Settings
- ##
- ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
- ssl_prefer_server_ciphers on;
- ##
- # Logging Settings
- ##
- access_log /var/log/nginx/access.log;
- error_log /var/log/nginx/error.log;
- ##
- # Gzip Settings
- ##
- gzip on;
- gzip_disable “msie6”;
- # gzip_vary on;
- # gzip_proxied any;
- # gzip_comp_level 6;
- # gzip_buffers 16 8k;
- # gzip_http_version 1.1;
- ##
- # Virtual Host Configs
- ##
- gzip on;
- gzip_disable “msie6”;
- # gzip_vary on;
- # gzip_proxied any;
- # gzip_comp_level 6;
- # gzip_buffers 16 8k;
- # gzip_http_version 1.1;
- # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
- ##
- # Virtual Host Configs
- ##
- include /etc/nginx/conf.d/*.conf;
- include /etc/nginx/sites-enabled/*;
- #以下為我們新增的內容
- server {
- listen 80;
- server_name your-ipaddress;
- root /home/my-project/;
- index index.html;
- location /datas {
- rewrite ^.+datas/?(.*)1 break;
- include uwsgi_params;
- proxy_pass http://ip:port;
- }
- }
- }
user www-data;
worker_processes auto;
pid /run/nginx.pid;
events {
worker_connections 768;
# multi_accept on;
}
http {
##
# Basic Settings
##
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
# server_tokens off;
# server_names_hash_bucket_size 64;
# server_name_in_redirect off;
include /etc/nginx/mime.types;
default_type application/octet-stream;
##
# SSL Settings
##
ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
ssl_prefer_server_ciphers on;
##
# Logging Settings
##
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
##
# Gzip Settings
##
gzip on;
gzip_disable "msie6";
# gzip_vary on;
# gzip_proxied any;
# gzip_comp_level 6;
# gzip_buffers 16 8k;
# gzip_http_version 1.1;
##
# Virtual Host Configs
##
gzip on;
gzip_disable "msie6";
# gzip_vary on;
# gzip_proxied any;
# gzip_comp_level 6;
# gzip_buffers 16 8k;
# gzip_http_version 1.1;
# gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
##
# Virtual Host Configs
##
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
#以下為我們新增的內容
server {
listen 80;
server_name your-ipaddress;
root /home/my-project/;
index index.html;
location /datas {
rewrite ^.+datas/?(.*)$ /$1 break;
include uwsgi_params;
proxy_pass http://ip:port;
}
}
}
接下來就根據配置檔案進行下一步工作。配置檔案中的server_name後面是阿里雲伺服器的ip地址
3.配置檔案中的listen是nginx監聽的埠號,所以需要在阿里雲伺服器上為80埠新增安全組規則
在本地的瀏覽器登入阿里雲伺服器->進入控制檯->點選安全組->點選配置規則->點選新增安全組規則,之後配置如下(注:入方向和出方向都要配置)
4.配置檔案中的root和index那兩行表示我們把專案資料夾放在/home/my-project下
例如有兩個專案資料夾分別為test1,test2,裡面都有index.html。則目錄結構如下
/home
|–my-project
|–test1
|–index.html
|–test2
|–index.html
則在瀏覽器輸入http://ip/test1/index.html
伺服器便會在/home/my-project中找到test1下的index.html執行;
如果在瀏覽器中輸入http://ip/test2/index.html
伺服器便會在/home/my-project中找到test2下的index.html執行;
這樣便可以在伺服器下放多個專案資料夾。
5.所以我們也需要在本地專案的config/index.js裡的build下進行修改,如果要把專案放到test1下,則
[javascript] view plain copy print?- assetsPublicPath: ‘/test1/’,
assetsPublicPath: '/test1/',
如果用到了vue-router,則修改/router/index.js
[javascript] view plain copy print?- exportdefaultnew Router({
- base: ’/test1/’, //新增這行
- linkActiveClass: ’active’,
- routes
- });
export default new Router({
base: '/test1/', //新增這行
linkActiveClass: 'active',
routes
});
6.nginx配置檔案中的location則是針對跨域處理,表示把對/datas的請求轉發給http://ip:port,本文中這個http://ip:port下就是需要的資料,例如http://ip:port/seller,在本地專案檔案中ajax請求資料的地方如下
[javascript] view plain copy print?- const url = ‘/datas/seller’;
- this.$http.get(url).then((response) => {
- …..
- });
const url = '/datas/seller';
this.$http.get(url).then((response) => {
.....
});
7.修改後在本地命令列下執行:cnpm run build 生成dist檔案。把dist檔案裡的index.html和static檔案上傳到伺服器的/home/my-project/test1下,目錄結構如下
/home
|–my-project
|–test1
|–index.html
|–static
8.啟動nginx
[plain] view plain copy print?- service nginx start
service nginx start
9.至此專案部署成功,在瀏覽器下輸入: http://ip/test1/index.html 即可