Preface
Based on development needs, you need to use Docker to install nginx and deploy multiple front-end projects.
1. Docker installs nginx container
docker pull nginx
# 挂载项目静态资源和配置文件
docker run --name nginx -p 8088:8088 \
-v /ruoyi/nginx/html:/usr/share/nginx/html \
-v /ruoyi/nginx/conf/nginx.conf:/etc/nginx/nginx.conf \
-v /ruoyi//nginx/conf.d:/etc/nginx/conf.d
-d nginx
2. Configure nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 8088;
server_name 192.168.31.131;
root /usr/share/nginx/html;
location /one {
alias /usr/share/nginx/html/one/;
try_files $uri $uri/ /one/index.html;
index index.html index.htm;
}
location /two {
alias /usr/share/nginx/html/two/;
try_files $uri $uri/ /two/index.html;
index index.html index.htm;
}
# 解决刷新浏览器 404问题
location @router {
rewrite ^.* /one/index.html last;
}
location /prod-api/{
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header REMOTE-HOST $remote_addr;
proxy_pass http://192.168.31.131:8081/;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
At this time, visit 192.168.31.131:8088/one
or 192.168.31.131:8088/tow
to access the respective projects.
3. Problems that may arise
For example: nginx
reverse proxy to one
project, when refreshing the page suddenly 404
problem occurs, It can be handled by configuring nginx.conf (the author uses the Vue2
project, and the routing is Router
).
# 解决刷新浏览器 404问题
location @router {
rewrite ^.* /one/index.html last;
}
As for how to balance the two
project, it is not yet clear. If you pass by, you can leave a message.