docker安装nginx 负载均衡配置

1、先run一个nginx

 1 sudo docker run --name nginx-test -p 8081:80 -d nginx 

2、再从启动的nginx中cp配置文件到本地

 1 sudo docker cp nginx-test-web:/etc/nginx/nginx.conf /home/nhsuser/nginx/conf 

3、挂载本地目录启动nginx
1 docker run -d -p 8082:80 --name nginx-test-web \
2 -v /home/nhsuser/nginx/www:/usr/share/nginx/html \
3 -v /home/nhsuser/nginx/conf/nginx.conf:/etc/nginx/nginx.conf \
4 -v /home/nhsuser/nginx/logs:/var/log/nginx nginx
4、docker上配置nginx负载均衡
 1 [root@SHH-HQ-NHS09S logs]# cat ../conf/nginx.conf 
 2 
 3 user  nginx;
 4 worker_processes  1;
 5 
 6 error_log  /var/log/nginx/error.log warn;
 7 pid        /var/run/nginx.pid;
 8 
 9 
10 events {
11     worker_connections  1024;
12 }
13 
14 
15 http {
16     include       /etc/nginx/mime.types;
17     default_type  application/octet-stream;
18 
19     log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
20                       '$status $body_bytes_sent "$http_referer" '
21                       '"$http_user_agent" "$http_x_forwarded_for"';
22 
23     access_log  /var/log/nginx/access.log  main;
24 
25     sendfile        on;
26     #tcp_nopush     on;
27 
28     keepalive_timeout  65;
29 
30     #gzip  on;
31 
32 # 注意,这里的server名字即org.tonny.balance不能带下划线,tomcat是上面安装的tomcat
33     upstream pcc {
34         #server 10.229.255.96:8889 weight=1;
35         server 10.229.255.96:8082 weight=1;
36     server 10.229.255.95:8089 weight=1;
37     }
38 
39     server {
40         #注意,这里保留80,因为前面的docker运行命令,会映射80端口
41       #  listen       80;
42       #  server_name  localhost;
43 
44         # 不带数据的请求
45         location / {
46             root   html;
47             index  index.html index.htm;
48             #访问映射
49             proxy_pass   http://pcc/; 
50         }
51 
52         #error_page  404              /404.html;
53 
54         # redirect server error pages to the static page /50x.html
55         #
56         error_page   500 502 503 504  /50x.html;
57         location = /50x.html {
58             root   html;
59         }
60     }
61 
62     include /etc/nginx/conf.d/*.conf;
63 }

5、测试

posted @ 2019-12-31 22:41  pcct  阅读(326)  评论(0)    收藏  举报