Background of the project:
1, the official website require revision, use nodejs nuxt framework for reconstruction
2, the official website revision is not completed, but need on-line Home
Project requirements:
1, so that the content appears as a new home project
2, so that the content Laoguan network can be accessed (such as www.n.com/cart, but no new projects cart Interface)
3 Home domain name must be www.n.com
In this case it will lead to some problems, such as back-end I nginx configuration, the current port is 7100, but the new project to port 3000, if I direct replacement location / port: 3000, so replacement is bound to make the old official website project 404
Old official website nginx configuration is as follows:
server { listen 80; listen 443 ssl; server_name www.n.com include ssl/niu.com; include error/40x; include error/50x; location / { proxy_pass http://127.0.0.1:7100; add_header 'Access-Control-Allow-Origin' '*'; add_header 'Access-Control-Allow-Credentials' 'true'; proxy_set_header X-Real-IP $remote_addr; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; } }
Resolution process:
Nginx start from a beginning attempt to write one location, locatin = / prot = 3000 location / port = 7100
Such resolve is more convenient solution to match 3000 ports, 3000 port match went less than 7100 ports, the configuration file as follows:
server { listen 80; listen 443 ssl; server_name www.n.com include ssl/niu.com; include error/40x; include error/50x; location = / { proxy_pass http://127.0.0.1:3000; add_header 'Access-Control-Allow-Origin' '*'; add_header 'Access-Control-Allow-Credentials' 'true'; proxy_set_header X-Real-IP $remote_addr; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; } location / { proxy_pass http://127.0.0.1:7100; add_header 'Access-Control-Allow-Origin' '*'; add_header 'Access-Control-Allow-Credentials' 'true'; proxy_set_header X-Real-IP $remote_addr; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; } }
but! Although this solution can configure the program successfully, but because of my front-end using nuxt framework that has a relatively axios plug-pit.
This plugin need to write the death domain: I wrote www.n.com, he will be found out if there is www.n.com dns go on, if found to, and also match their node process on, and will visit a success otherwise, it will error 404.
So based on this location priority configuration, if I write a location = / port = 3000 this mode, the back-end node will hang directly, that will be reported 404, page inaccessible.
Thus there is no way to achieve the above-described way on nuxt frame. . .
This time we need a second solution:
When nginx to match 404 when automatically go to the old official online query, if the query came, it returns the result.
Configuration is as follows
location / { proxy_pass http://127.0.0.1:3000; add_header 'Access-Control-Allow-Origin' '*'; add_header 'Access-Control-Allow-Credentials' 'true'; proxy_set_header X-Real-IP $remote_addr; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; #新网站报错,跳转到老网站 2019.06.02 error_page 404 = @old_niu; } #新网站报错,跳转到老网站 2019.06.02 location @old_niu{ OFF proxy_redirect; Host Host $ proxy_set_header; proxy_set_header the X-Forwarded-the For-$ proxy_add_x_forwarded_for; proxy_pass http://127.0.0.1:7100; # here old system configuration error page }
So that you can go to the official website of the new match, and if an error 404, go to the official website of the old, can be considered a solution.