How to deploy multiple Vue projects under the same domain name using nginx and use reverse proxy

How to deploy multiple Vue projects under the same domain name using nginx and use reverse proxy

Effect

There are currently 2 projects (project1, project2), and an index.html that comes with nginx. I added the corresponding link code (pasted later) to unify the routing of the sub-projects.

I expect to achieve the following effect (assuming ip: localhost, port: 8080):

http://localhost:8080/ Enter the outermost index.html
http://localhost:8080/project1 Enter project 1
http://localhost:8080/project2 Enter project 2

Without further ado, let's start configuring

Vue Configuration

I am using vue-cli2 to build the project, so I need to modify some vue configuration parameters accordingly.

index.js in the config folder is packaged, so we need to change the corresponding project name in build.assetsPublicPath, for example

//project1
module.exports = {
 dev: {},
 build: {
  assetsPublicPath: '/project1/' // Note the leading and trailing '/'
 }
}

//project2
module.exports = {
 dev: {},
 build: {
  assetsPublicPath: '/project2/' // Note the leading and trailing '/'
 }
}

Modify prod.env.js in the config folder to this:

//project1
module.exports = {
 NODE_ENV: '"production"',
 BASE_API: '"/api/pro1"' // This will correspond to the nginx configuration later}

//project2
module.exports = {
 NODE_ENV: '"production"',
 BASE_API: '"/api/pro2"' // This will correspond to the nginx configuration later}

[Note] Because I use BASE_API as the proxy prefix in my project, if yours is not here, you need to find your own proxy configuration

Because everyone's vue-router file configuration is different, you need to find your router configuration file and modify it internally:

// I used the history mode, I didn't test the hash mode, I think it should have the same effect // project1
export default new Router({
 base: '/project1/', // Note to change your subproject name, this corresponds to your build.assetsPublicPath
 mode: 'history',
 scrollBehavior: () => ({ y: 0 }),
 routes: []
})

//project2
export default new Router({
 base: '/project2/', // Note to change your subproject name, this corresponds to your build.assetsPublicPath
 mode: 'history',
 scrollBehavior: () => ({ y: 0 }),
 routes: []
})

[Note] In npm run build, you may get an error such as .tap(*). This is because there is a problem with the html-webpack-plugin version in the package. You can execute the following statement

# This version is the version in your package.json, but you need to re-specify this version$ npm i [email protected] -D

Nginx Configuration

First of all, my directory is like this, all irrelevant files are shown as...

.
├─conf
│ ├─... # Other files│ └─nginx.conf
│
├─html # Just look here, I don’t use the others for now│ ├─project1
│ │ └─static
│ │ ├─css
│ │ ├─fonts
│ │ └─js
│ │ ├─g
│ │ └─V
│ ├─project2
│ │ └─static
│ │ ├─css
│ │ ├─fonts
│ │ └─js
│ │ ├─g
│ │ └─V
│ ├─index.html
│ └─50x.html
└─... # Other files

[Explanation] My nginx directory is native, and it contains an html folder. To save trouble, I use this directly. Of course, you can also specify other directories, but for now please use the same configuration as mine, and you can customize it later.

Now we start to configure the nginx.conf file in the conf folder

I modified the original file directly, and the modified configurations were all in the http module, so I replaced other unnecessary codes with...

# ...
# Reverse proxy 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"';

  sendfile on;
  keepalive_timeout 65;

  client_max_body_size 20M;
  client_body_buffer_size 10M;
  large_client_header_buffers 4 128k;
  
  # This can be used as a cluster upstream p1_server {
    server localhost:8081;
  }

  # This can be used as a cluster upstream p2_server {
    server localhost:8082;
  }

  server {
    listen 8080;
    server_name localhost;
    charset utf-8;

    proxy_connect_timeout 180;
    proxy_send_timeout 180;
    proxy_read_timeout 180;
    proxy_set_header Host $host;
    proxy_set_header X-Forwarder-For $remote_addr;

    root html; # Here we specify our folder # The total project route, I was lazy and wrote it directly in the same file # If there are many, you can configure multiple conf files and use include to associate them location / {
      try_files $uri $uri/ /index.html; # Here you can understand that it is specified to the index.html in the html folder
    }
    
    #project1
    # Here is the build.assetsPublicPath configuration of config/index.js in the vue project just now.
    # It is also the base of the router configured in the Vue project
    location ^~ /project1 {
      try_files $uri $uri/ /project1/index.html; # Here you can understand that it is specified to the index.html of the project1 folder under the html folder
    }
    
    #project2
    # Here is the configuration location of project 2 ^~ /project2 { # 
      try_files $uri $uri/ /project2/index.html; # Here you can understand that it is specified to the index.html of the project2 folder under the html folder
    }
    
    # Here is the interface that project1 needs to call for configuration location /api/pro1 { # Here is the configuration BASE_API of prod.env.js in the vue project 
      proxy_redirect off;
      proxy_set_header Host $host;
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_pass http://p1_server; # The p1_server here corresponds to the above configuration upstream p1_server {}, here you can do clustering, I don't need it, so I just configure it simply}
    
     # Here is the interface that project1 needs to call for configuration location /api/pro2 { # Here is the configuration BASE_API of prod.env.js in the vue project
      proxy_redirect off;
      proxy_set_header Host $host;
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_pass http://p2_server; # The p2_server here corresponds to the above configuration upstream p2_server {}, here you can do clustering, I don't need it, so I just configure it simply}

    # ...
  }

  # ...
}

Finally, I posted the code of index.html that I modified

Because I am appending, I will directly post the code I appended, and the others will be...

...
<p><em>Thank you for using nginx.</em></p> <!-- For display location-->

<!-- start: append -->
<hr>
<a href="/project1" rel="external nofollow" >Project 1</a> | <a href="/project2" rel="external nofollow" >Project 2</a>
<!-- end: append-->

</body> <!-- For display purposes -->

Final debugging

After all the configurations are completed, we can start nginx. If you don’t know how to do this, please solve it yourself.

The startup is successful. We can see it by entering http://localhost:8080 in the browser.

Click on project 1, and we can see that the link becomes http://localhost:8080/project1

Click on Project 2, the link changes to http://localhost:8080/project2, which is exactly what we expected, and it is successful.

[Forced explanation of metaphysics] I configured it that day, but it gave an error as soon as I started it, so I finally gave up. But the next day, when I was ready to check, everything was fine when I started it. I was so confused! If you encounter the same problem as me, just leave it alone, maybe it will be fixed the next day.

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • How to deploy Vue project on nginx (history mode)
  • How to deploy Vue project under nginx
  • Solution to the problem of not finding js css files when deploying vue project with nginx
  • Solve the problem of refreshing blank when deploying Vue project nginx to non-root directory
  • Implementation of deploying vue project to nginx/tomcat server
  • Vue.js project nginx deployment tutorial
  • How to solve the problem that CSS style does not take effect when Vue is packaged and deployed to Nginx
  • The server uses Nginx to deploy the Vue project

<<:  Detailed explanation of CocosCreator project structure mechanism

>>:  Detailed explanation of the general steps for SQL statement optimization

Recommend

Debian virtual machine created by VirtualBox shares files with Windows host

the term: 1. VM: Virtual Machine step: 1. Downloa...

A detailed introduction to HTML page loading and parsing process

The order in which the browser loads and renders H...

Use jQuery to fix the invalid page anchor point problem under iframe

The application scenario is: the iframe page has n...

MySQL master-slave replication principle and practice detailed explanation

Table of contents Introduction effect principle f...

Details of MutationObServer monitoring DOM elements in JavaScript

1. Basic Use It can be instantiated through the M...

Basic security settings steps for centos7 server

Turn off ping scanning, although it doesn't h...

Linux beginners in virtual machines configure IP and restart the network

For those who are new to virtual machines or have...

HTML table markup tutorial (1): Creating a table

<br />This is a series of tutorials provided...

Copy and paste is the enemy of packaging

Before talking about OO, design patterns, and the ...

Detailed Tutorial on Installing MySQL 5.7 on RedHat 6.5

RedHat6.5 installation MySQL5.7 tutorial sharing,...

How to completely delete and uninstall MySQL in Windows 10

Preface This article introduces a tutorial on how...

Detailed explanation of common methods of JavaScript Array

Table of contents Methods that do not change the ...

JavaScript object-oriented implementation of magnifying glass case

This article shares the specific code of JavaScri...

Solution to index failure in MySQL due to different field character sets

What is an index? Why create an index? Indexes ar...