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

相关推荐
若云止水39 分钟前
Ubuntu 下 nginx-1.24.0 源码分析 - ngx_init_cycle 函数 - 详解(4)
数据库·nginx·ubuntu
Dontla1 小时前
Docker run --add-host参数解析(在容器启动时向/etc/hosts文件中添加自定义的主机名与IP映射)(适用于临时调试或测试)
tcp/ip·docker·容器
西西小飞龙1 小时前
Docker和K8S中pod、services、container的介绍和关系
docker·容器·kubernetes
元气满满的热码式1 小时前
Docker数据卷操作实战
linux·运维·docker·云原生·容器
kngines1 小时前
【实战 ES】实战 Elasticsearch:快速上手与深度实践-1.3.1单节点安装(Docker与手动部署)
大数据·elasticsearch·docker
浪九天4 小时前
Nginx系列05(负载均衡、动静分离)
nginx·运维开发·持续部署
m0_7482365811 小时前
Nginx中$http_host、$host、$proxy_host的区别
运维·nginx·http
飞火流星0202712 小时前
docker安装etcd:docker离线安装etcd、docker在线安装etcd、etcd镜像下载、etcd配置详解、etcd常用命令、安装常见问题总结
docker·容器·etcd
web1478621072313 小时前
Linux(CentOS)安装 Nginx
linux·nginx·centos