Table of Contents
Usage scenarios:
1. Packaging
2. Deployment (nginx)
Home Operation and Maintenance Nginx How to package Vue project and deploy nginx server

How to package Vue project and deploy nginx server

May 15, 2023 am 08:16 AM
vue nginx

Usage scenarios:

When we often use front-end and back-end separation projects, we will need to package the front-end vue and then deploy it.

1. Packaging

vue projects can actually be packaged directly through the following statement:

1

npm run build

Copy after login

The default packaging situation is as follows:

How to package Vue project and deploy nginx server

How to package Vue project and deploy nginx server

When we need to modify the packaging name and static resource location, we need to configure it accordingly:

1. First create vue.config in the project root directory .js file

How to package Vue project and deploy nginx server

The configuration content is as follows (with cross-domain problem resolution included):

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

module.exports = {

    //打包

    publicPath: './',

    outputDir: 'test', //打包输出目录

    assetsDir: './static', //放置生成的静态资源

    filenameHashing: true, // 生成的静态资源在它们的文件名中包含了 hash 以便更好的控制缓存

    lintOnSave: false, //设置是否在开发环境下每次保存代码时都启用 eslint验证

    productionSourceMap: false,// 打包时不生成.map文件

     

    // 解决跨域配置

    devServer: {                //记住,别写错了devServer//设置本地默认端口  选填

        port: 8080,

        proxy: {                 //设置代理,必须填

            '/api': {              //设置拦截器  拦截器格式   斜杠+拦截器名字,名字可以自己定

                target: 'http://localhost:9090',     //代理的目标地址(后端设置的端口号)

                changeOrigin: true,              //是否设置同源,输入是的

                pathRewrite: {                   //路径重写

                    '/api': ''                     //选择忽略拦截器里面的单词

                }

                /*也就是在前端使用/api可以直接替换为(http://localhost:9090)*/

            }

        }

    },

}

Copy after login

2. View routing (router/index.js) Whether to use history, if so, change it to hash. Or just uncheck mode because hash is used by default.

1

2

3

4

5

6

7

const router = new VueRouter({

  /*mode: 'history',*/

  mode: 'hash',

  routes:[]

})

  

export default router

Copy after login

Then use npm run build again to package, and the test folder will appear, and the static files will be placed in static.

The packaging has ended.

3. Find the path of the packaged file

Double-click the packaged index.html file, and you will see the homepage.

2. Deployment (nginx)

First you need to install nignx, which is undoubtedly not introduced here. (Or specific installation steps will be placed in the nginx section later)

Just configure it directly in nginx.conf:

1

2

3

4

5

6

7

8

server {

    listen   8021;

    server_name  localhost;

  

    location /test{

        alias    /home/hyq/vue_file;

        index  index.shtml index.html index.htm;

    }

Copy after login

The specific meaning of the configuration:

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

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

#user  nobody;

worker_processes  1;

 

#error_log  logs/error.log;

#error_log  logs/error.log  notice;

#error_log  logs/error.log  info;

 

#pid        logs/nginx.pid;

 

 

events {

    worker_connections  1024;

}

 

http {

    include       mime.types;

    default_type  application/octet-stream;

 

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '

    #                  '$status $body_bytes_sent "$http_referer" '

    #                  '"$http_user_agent" "$http_x_forwarded_for"';

 

    #access_log  logs/access.log  main;

 

    sendfile        on;

    #tcp_nopush     on;

 

    #keepalive_timeout  0;

    keepalive_timeout  65;

 

    #gzip  on;

     

    ssi on;

    ssi_silent_errors on;

    ssi_types text/shtml;

     

    #定义的服务器列表

    upstream cms {

        #这里代表代理的项目端口为127.0.0.1:8111端口(weight等配置自行查询)

        server 127.0.0.1:8111 weight=5 max_fails=3 fail_timeout=20s;

    }

    server {

        listen       8096;              #nginx使用8096

        server_name  localhost;         #服务名称

 

        location /menhu/cms {

            proxy_pass http://cms;     

            #请求转向cms 定义的服务器列表。也就是访问localhost:8096/menhu/cms 会转向到上方服务器列          #表中的127.0.0.1:8111

        }  

 

        location /qgxzzfzhgljdpt {

            root   D:\BDWorkParce3\LPT_MENHU\wwwroot_release;   #根目录

            index  index.shtml index.html index.htm;            #设置默认页

            #访问localhost:8096/qgxzzfzhgljdpt 会打开        D:\BDWorkParce3\LPT_MENHU\wwwroot_release\qgxzzfzhgljdpt下级中的index.shtml/index.html/index.htm默认页

        }

        location ^~ /template {

            return 404;

        }

        location = /c/ {

            return 404;

        }

        location = /css/ {

            return 404;

        }

        location = /images/ {

            return 404;

        }

        location = /include/ {

            return 404;

        }

        location = /js/ {

            return 404;

        }

        location = /style/ {

            return 404;

        }

        location = /upload/ {

            return 404;

        }

        location = /html/ {

            return 404;

        }

        location = /root/ {

            return 404;

        }

        location ~ .*.(svn|Git|git) {

            return 404;

        }

 

        error_page   500 502 503 504  /50x.html;

        location = /50x.html {

            root   html;

        }

    }

 

}

########### 每个指令必须有分号结束。#################

#user administrator administrators;  #配置用户或者组,默认为nobody nobody。

#worker_processes 2;  #允许生成的进程数,默认为1

#pid /nginx/pid/nginx.pid;   #指定nginx进程运行文件存放地址

error_log log/error.log debug;  #制定日志路径,级别。这个设置可以放入全局块,http块,server块,级别以此为:debug|info|notice|warn|error|crit|alert|emerg

events {

    accept_mutex on;   #设置网路连接序列化,防止惊群现象发生,默认为on

    multi_accept on;  #设置一个进程是否同时接受多个网络连接,默认为off

    #use epoll;      #事件驱动模型,select|poll|kqueue|epoll|resig|/dev/poll|eventport

    worker_connections  1024;    #最大连接数,默认为512

}

http {

    include       mime.types;   #文件扩展名与文件类型映射表

    default_type  application/octet-stream; #默认文件类型,默认为text/plain

    #access_log off; #取消服务日志   

    log_format myFormat '$remote_addr$remote_user [$time_local] $request $status $body_bytes_sent $http_referer $http_user_agent $http_x_forwarded_for'; #自定义格式

    access_log log/access.log myFormat;  #combined为日志格式的默认值

    sendfile on;   #允许sendfile方式传输文件,默认为off,可以在http块,server块,location块。

    sendfile_max_chunk 100k;  #每个进程每次调用传输数量不能大于设定的值,默认为0,即不设上限。

    keepalive_timeout 65;  #连接超时时间,默认为75s,可以在http,server,location块。

 

    upstream mysvr {  

      server 127.0.0.1:7878;

      server 192.168.10.121:3333 backup;  #热备

    }

    error_page 404 https://www.baidu.com; #错误页

    server {

        keepalive_requests 120; #单连接请求上限次数。

        listen       4545;   #监听端口

        server_name  127.0.0.1;   #监听地址      

        location  ~*^.+$ {       #请求的url过滤,正则匹配,~为区分大小写,~*为不区分大小写。

           #root path;  #根目录

           #index vv.txt;  #设置默认页

           proxy_pass  http://mysvr;  #请求转向mysvr 定义的服务器列表

           deny 127.0.0.1;  #拒绝的ip

           allow 172.18.5.54; #允许的ip          

        }

    }

}

Copy after login

Then start or Just restart nginx.

Visit: server address: 8021/test.

The above is the detailed content of How to package Vue project and deploy nginx server. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to enable pseudostatic in nginx How to enable pseudostatic in nginx Apr 14, 2025 am 08:21 AM

How to enable Nginx pseudostatic? Enable the MultiViews module to allow the server to search for files based on the extension. Add a pseudo-static rule to rewrite the .php URL to the matching PHP file. Ensure that the web server process has permission to access PHP files. Restart Nginx to apply changes to implement the URL map to file paths.

How to configure load balancing in nginx How to configure load balancing in nginx Apr 14, 2025 am 08:33 AM

How to configure Nginx for load balancing? Defines the upstream server pool and specifies the server IP and port. Define virtual hosts, listen for connections and forward them to the upstream pool. Specify the location, match the request and forward it to the upstream pool.

How to check the running status of nginx How to check the running status of nginx Apr 14, 2025 am 11:48 AM

The methods to view the running status of Nginx are: use the ps command to view the process status; view the Nginx configuration file /etc/nginx/nginx.conf; use the Nginx status module to enable the status endpoint; use monitoring tools such as Prometheus, Zabbix, or Nagios.

How to view nginx version information How to view nginx version information Apr 14, 2025 am 08:24 AM

View Nginx version information through the following method: Direct command method: "nginx -v" output version information. View in the configuration file: Find the "version" section at the top of the configuration file. System information command: Linux: Use the "rpm -qa | grep nginx" or "dpkg -l | grep nginx" command. FreeBSD: Use the "pkg info nginx" command. Windows: Open Nginx service properties, version information is located in the General tab.

How to redirect in nginx How to redirect in nginx Apr 14, 2025 am 08:42 AM

Methods for redirecting through Nginx are 301 permanent redirects (update links or mobile pages) and 302 temporary redirects (handling errors or temporary changes). Configuring redirection involves using location directives in server blocks, advanced features include regular expression matching, proxy redirection, and condition-based redirection. Common uses of redirects include updating URLs, handling errors, redirecting HTTP to HTTPS, and guiding users to a specific country or language version.

How to build a website in nginx How to build a website in nginx Apr 14, 2025 am 11:21 AM

Using Nginx to build a website is carried out in five steps: 1. Install Nginx; 2. Configure Nginx, mainly configuring the listening port, website root directory, index file and error page; 3. Create website files; 4. Test Nginx; 5. Advanced configuration can be carried out as needed, such as SSL encryption, reverse proxy, load balancing and caching.

How to set nginx access address to server ip How to set nginx access address to server ip Apr 14, 2025 am 11:36 AM

To set the access address to server IP in Nginx, configure the server block, set the listening address (such as listen 192.168.1.10:80) Set the server name (such as server_name example.com www.example.com), or leave it blank to access the server IP and reload Nginx to apply the changes

How to enable stream in nginx How to enable stream in nginx Apr 14, 2025 am 09:45 AM

How to enable Nginx's Stream module? Enabling the Stream module requires six steps: Installing the Stream module configuration Nginx Create Stream Server Block Configuration Stream Server Options Restart Nginx Verification Enable

See all articles