docker配置nginx,并部署多个项目

前言

基于开发需要需要使用Docker安装nginx,并部署多个前端项目。

1、docker安装nginx容器

shell 复制代码
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、配置nginx.conf

shell 复制代码
worker_processes  1;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;

    server {
        listen       8080;
        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;
        }
    }
}

此时访问 192.168.31.131:8080/one或者192.168.31.131:8080/tow,就可以访问各自的项目了。

3、其中可能会出现的问题

例如:nginx反向代理到one项目后,在刷新页面突然出现404的问题时,可以通过配置nginx.conf来处理(笔者使用的Vue项目,路由是Router)。

shell 复制代码
# 解决刷新浏览器 404问题
location @router {
	rewrite ^.* /one/index.html last;
} 

至于如何兼顾 two项目,暂时尚未清楚,大神路过可以留言。

相关推荐
代码老y2 小时前
Docker:容器化技术的基石与实践指南
运维·docker·容器
DuelCode3 小时前
Windows VMWare Centos Docker部署Springboot 应用实现文件上传返回文件http链接
java·spring boot·mysql·nginx·docker·centos·mybatis
杨浦老苏7 小时前
开源服务运行监控工具Lunalytics
docker·群晖·网站监控
dyj09511 小时前
【Rancher Server + Kubernets】- Nginx-ingress日志持久化至宿主机
运维·nginx·rancher
PanZonghui12 小时前
Centos项目部署之Nginx部署项目
linux·nginx
Hellc00713 小时前
Nginx 高级 CC 与 DDoS 防御策略指南
运维·nginx·ddos
呆萌的代Ma13 小时前
解决Mac上的老版本docker desktop 无法启动/启动后一直转圈/无法登陆账号的问题
macos·docker·eureka
feilieren13 小时前
Docker 安装 Elasticsearch 9
运维·elasticsearch·docker·es
KaiwuDB14 小时前
使用Docker实现KWDB数据库的快速部署与配置
数据库·docker
小皮侠14 小时前
nginx的使用
java·运维·服务器·前端·git·nginx·github