1. How to deploy
In the separate delivery mode of front and back ends, the front and back ends are independently deployed, and the front end only needs to upload the last build to the target server.web
It can be found in the static directory specified by the container
We knowvue
After the project is built, a series of static files are generated.
We only need to upload this directory to the target server for regular deployment
// Scp upload User is the host login user, host is the host external network ip, and xx is the static resource path of the web containerscp user@host:/xx/xx/xx
letweb
The container runs,nginx
As an example
server { listen 80; server_name ; location / { index /data/dist/; } }
Remember to restart after the configuration is completednginx
// Check if the configuration is correctnginx -t // Smooth restartnginx -s reload
After the operation is completed, you can enter the domain name in the browser to access it
Of course, the above is just the simplest and most direct way of deployment.
What automation, mirroring, container, and assembly line deployment are essentially abstracting, isolating, and replacing repetitive labor with programs. This article will not expand on
2. Question 404
This is a classic question. I believe many students have encountered it. Do you know the real reason?
Let's restore the scene first:
-
vue
The project runs normally when it is local, but it is deployed to the server and refreshes the page and has a 404 error
First, the HTTP 404 error means that the resource pointed to by the link does not exist.
The question is why it doesn't exist? And why onlyhistory
Will this problem occur in mode?
Why are there problems in history mode
Vue
It is a single-page application
andSPA
It is a model of a web application or website. All user interactions are dynamically rewriting the current page. We also saw it before. No matter how many pages our application has, the building will only produce one.
Now, let's look back at oursnginx
Configuration
server { listen 80; server_name ; location / { index /data/dist/; } }
Can be based onnginx
Configuration is obtained when we enter the address barWhen we are, we will open
dist
In the directory File, then we jump to the route
/login
The key is here, when we are/login
The page is refreshed.nginx location
There is no relevant configuration, so there will be a 404 situation
Why is there no problem in hash mode
router hash
We all know that the pattern is represented by symbols #, such as/#/login
, hash
The value of#/login
Its characteristics are:hash
Although it appears inURL
but will not be included inHTTP
During the request, it has no effect on the server, so it changeshash
The page will not be reloaded
hash
In mode, onlyhash
The content before the symbol will be included in the request, such as/#/login
onlyWill be included in the request, so for the server, even if there is no configuration
location
, will not return a 404 error
Solution
After seeing this, I believe most students can think of how to solve the problem.
The essence of the problem is that our routes perform view switching through JS.
When we enter the subroutine, refresh the page.web
The container has no corresponding page, and then 404 will appear
So we just need to configure the redirection of any page to, hand over the route to the front-end to process
rightnginx
Configuration File.conf
Modify, addtry_files $uri $uri/ /;
server { listen 80; server_name ; location / { index /data/dist/; try_files $uri $uri/ /; } }
Remember to update the configuration after modifying the configuration file
nginx -s reload
After doing this, your server will no longer return the 404 error page, because it will be returned for all paths.document
To avoid this, you shouldVue
The application covers all routing situations, and then gives a 404 page
const router = new VueRouter({ mode: 'history', routes: [ { path: '*', component: NotFoundComponent } ] })
Regarding the backend configuration plan, there are also:Apache
、nodejs
Wait, the ideas are consistent, so I won't go into the discussion here
References
- /post/6844903872637632525
- /topic/5f8cf91d96b2cb0032c385c0
Summarize
This is the article about the solution of the 404 error after the local development of the vue project is completed and the 404 error is reported. For more related content of vue deployment to the server, please search for my previous article or continue browsing the related articles below. I hope everyone will support me in the future!