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项目,暂时尚未清楚,大神路过可以留言。

相关推荐
闲云野鹤在人间9 小时前
Kubernetes Service 与 kube-proxy 详解
云原生·容器·kubernetes
说实话起个名字真难啊10 小时前
实战docker部署dify及注意点
运维·docker·容器
闲云自留地11 小时前
K8s Service 全解:服务发现、四种类型、会话保持、金丝雀发布一篇通
容器·kubernetes·服务发现
choumin12 小时前
C++ 标准容器为什么不能存引用
c++·容器·引用类型
Starry-sky(jing)13 小时前
crane 下载镜像:不连接 Docker,3 步准备并校验 ARM64 离线包
运维·docker·arm64·crane
对象存储与RustFS14 小时前
GPU 在空转等数据?PyTorch 训练集进 RustFS 的三种接法
后端·docker·云原生
庵中十三居士15 小时前
用nginx代理DeepSeekHarness挂在域名下特定路径提供访问
运维·nginx·deepseek·harness
fb_1234517 小时前
K8s 网络通关手册:Ingress-Nginx 生产级路由 + Calico 三种隧道模式 + NetworkPolicy 白名单实战
网络·nginx·kubernetes
承渊政道17 小时前
新增一台服务器时还要重新装Nginx?Ansible只改hosts目标列表,同一份Playbook继续执行
运维·nginx·ansible·内网穿透·cpolar
KIDULT°17 小时前
K8s 网络进阶实战:Ingress-Nginx、Calico 与 NetworkPolicy 实验 + 排坑记录
网络·nginx·云原生·kubernetes