查看默认配置
1
| docker --rm --entrypoint=cat nginx /etc/nginx/nginx.conf
|
--rm 表示运行完就删掉容器--entrypoint=cat 表示替换掉容器的默认命令,使用cat命令
导出默认配置到宿主机当前运行目录
1
| docker --rm --entrypoint=cat nginx /etc/nginx/nginx.conf > myconf.nginx
|
服务端真实地址是 http://localhost:8080/admin/ 我们希望隐藏掉服务器地址和/admin这个路径,使得访问地址变成 http://localhost/api,那我们就可以匹配这个路径,设置代理服务器。
1
2
3
4
| # 反向代理,处理管理端发送的请求。http://localhost/api --> http://localhost:8080/admin
location /api/ {
proxy_pass http://localhost:8080/admin/;
}
|
在本地调试的时候,如果想访问宿主机springboot运行的后端8080端口,无法在docker上使用localhost访问,查阅文档知,容器可以通过host.docker.internal 访问宿主机ip。所以我们可以写成
1
2
3
4
5
6
| # 反向代理,处理前端发送的请求到宿主机
# http://localhost/api --> http://host.docker.internal:8080/admin
location /api/ {
# proxy_pass http://localhost:8080/admin/;
proxy_pass http://host.docker.internal:8080/admin/;
}
|
例如有两台后端服务器,分别是 ip1:8080 与ip2:8080,配置如下,weight是权重配置
1
2
3
4
| upstream webservers{
server ip1:8080 weight=90;
server ip2:8088 weight=10;
}
|
设置完之后别忘了修改反向代理服务器为负载均衡upstream 指定的名称webservers
1
2
3
4
5
| # 反向代理,处理管理端发送的请求
location /api/ {
# proxy_pass http://localhost:8080/admin/;
proxy_pass http://webservers/admin/;
}
|
使用-v指令挂载宿主机html目录与nginx.conf文件
1
| docker run --rm -it -p 80:80 -v C:\\Users\\Administrator\\docker\\cangqiongwaimai\\nginx1.20.0\\html:/etc/nginx/html -v C:\\Users\\Administrator\\docker\\cangqiongwaimai\\nginx1.20.0\\conf\\nginx.conf:/etc/nginx/nginx.conf nginx:1.20.2
|
由于反斜杠在yml中会被识别为转义字符,所以只能用/ 替代
1
2
3
4
5
6
7
8
9
10
11
| version: '3.8'
services:
nginx:
image: nginx:1.20.2
ports:
- "80:80"
volumes:
# 修正路径分隔符 + 确保文件存在
- "C:/Users/Administrator/docker/dockercompose/cangqiongwaimai/nginx1.20.0/conf/nginx.conf:/etc/nginx/nginx.conf"
# 修正HTML目录挂载位置
- "C:/Users/Administrator/docker/dockercompose/cangqiongwaimai/nginx1.20.0/html:/usr/share/nginx/html"
|
也可以用相对目录
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
| services:
nginx:
image: nginx:1.20.0-alpine # 推荐使用alpine版本
restart: unless-stopped
ports:
- "80:80"
volumes:
- ./conf/nginx.conf:/etc/nginx/nginx.conf:ro
- ./html:/etc/nginx/html:ro
- ./logs:/var/log/nginx
networks:
- cangqiongwaimai
networks:
cangqiongwaimai:
|
dockercompose 运行命令
-f docker-compose.yml 指定配置文件为docker-compose.yml,如果配置文件名称就是docker-compose.yml 可以省略up 启动服务-d 和docker run -d 命令一样,是后台运行
1
| docker compose -f docker-compose.yml up -d
|
停止服务
1
| docker compose -f docker-compose.yml down
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
|
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
map $http_upgrade $connection_upgrade{
default upgrade;
'' close;
}
upstream webservers{
server host.docker.internal:8080 weight=90 ;
#server 127.0.0.1:8088 weight=10 ;
}
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html/sky;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# 反向代理,处理管理端发送的请求
location /api/ {
# proxy_pass http://localhost:8080/admin/;
proxy_pass http://host.docker.internal:8080/admin/;
#proxy_pass http://webservers/admin/;
}
# 反向代理,处理用户端发送的请求
location /user/ {
proxy_pass http://webservers/user/;
}
# WebSocket
location /ws/ {
proxy_pass http://webservers/ws/;
proxy_http_version 1.1;
proxy_read_timeout 3600s;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "$connection_upgrade";
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
|