vue.js前端项目部署到nginx服务器

原文出处:https://blog.csdn.net/sherry_chan/article/details/79055211



注:

  • 本文选择了nginx做web服务器。
  • 因为在本文vue前端项目中,需要从接口获取数据,可是数据与vue前端项目是在同一ip地址但不同端口号,所以需要跨域读取数据。本文的跨域处理是使用了nginx。

环境:


  • 阿里云服务器(ubuntu 64bit)
  • xshell
  • nginx

1.使用xshell登录到阿里云服务器。安装nginx(本文安装到/etc下)

  1. cd /etc  
  2. apt-get update  
  3. apt-get install nginx  
cd /etc
apt-get update
apt-get install nginx

2.首先先配置nginx,然后再根据配置文件做下一步操作

打开/etc/nginx/nginx.conf文件

  1. vim /etc/nginx/nginx.conf  
vim /etc/nginx/nginx.conf
在nginx.conf中配置如下:

  1. user www-data;  
  2. worker_processes auto;  
  3. pid /run/nginx.pid;  
  4. events {  
  5.         worker_connections 768;  
  6.         # multi_accept on;  
  7. }  
  8. http {  
  9.   
  10.         ##  
  11.         # Basic Settings  
  12.         ##  
  13.   
  14.         tcp_nodelay on;  
  15.         keepalive_timeout 65;  
  16.         types_hash_max_size 2048;  
  17.         # server_tokens off;  
  18.   
  19.         # server_names_hash_bucket_size 64;  
  20.         # server_name_in_redirect off;  
  21.   
  22.         include /etc/nginx/mime.types;  
  23.         default_type application/octet-stream;  
  24.   
  25.         ##  
  26.         # SSL Settings  
  27.         ##  
  28.   
  29.         ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE  
  30.         ssl_prefer_server_ciphers on;  
  31.   
  32.         ##  
  33.         # Logging Settings  
  34.         ##  
  35.   
  36.         access_log /var/log/nginx/access.log;  
  37.         error_log /var/log/nginx/error.log;  
  38.   
  39.         ##  
  40.         # Gzip Settings  
  41.         ##  
  42.   
  43.         gzip on;  
  44.         gzip_disable “msie6”;  
  45.   
  46.         # gzip_vary on;  
  47.         # gzip_proxied any;  
  48.         # gzip_comp_level 6;  
  49.         # gzip_buffers 16 8k;  
  50.         # gzip_http_version 1.1;  
  51.   
  52.         ##  
  53.         # Virtual Host Configs  
  54.         ##  
  55.   
  56.   
  57.         gzip on;  
  58.         gzip_disable “msie6”;  
  59.   
  60.         # gzip_vary on;  
  61.         # gzip_proxied any;  
  62.         # gzip_comp_level 6;  
  63.         # gzip_buffers 16 8k;  
  64.         # gzip_http_version 1.1;  
  65.         # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;  
  66.   
  67.         ##  
  68.         # Virtual Host Configs  
  69.         ##  
  70.   
  71.         include /etc/nginx/conf.d/*.conf;  
  72.         include /etc/nginx/sites-enabled/*;  
  73.         #以下为我们添加的内容  
  74.        server {               
  75.               listen 80;  
  76.               server_name your-ipaddress;  
  77.   
  78.               root /home/my-project/;  
  79.               index index.html;  
  80.               location /datas {  
  81.               rewrite ^.+datas/?(.*) / /1 break;  
  82.               include uwsgi_params;  
  83.               proxy_pass http://ip:port;  
  84.                               }  
  85.              }  
  86. }  
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 ?
  1. assetsPublicPath: ‘/test1/’,  
assetsPublicPath: '/test1/',
如果用到了vue-router,则修改/router/index.js

[javascript] view plain copy
print ?
  1. export default new Router({  
  2.   base: ’/test1/’,   //添加这行  
  3.   linkActiveClass: ’active’,  
  4.   routes  
  5. });  
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 ?
  1. const url = ‘/datas/seller’;  
  2. this.$http.get(url).then((response) => {  
  3.   …..  
  4. });  
      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

  1. service nginx start  
service nginx start
9.至此项目部署成功,在浏览器下输入:  http://ip/test1/index.html  即可