Docker學習 - nginx配置

1.不挂載volumn的方式啓動image
docker run -d -p 80:80 --name nginx-test nginx
2.拷貝文件完後停止container docker cp nginx-test:/etc/nginx/.
/usr/local/docker/nginx/conf
docker stop nginx-test
docker rm nginx-test
目錄結構如下

[centos@centos7 ~]$ tree /usr/local/docker
/usr/local/docker
└── nginx
├── conf
│   ├── conf.d
│   │   └── default.conf
│   ├── fastcgi_params
│   ├── mime.types
│   ├── nginx.conf
│   ├── scgi_params
│   └── uwsgi_params
├── html
│   └── index.html
└── logs
├── access.log
└── error.log

3.挂載本地目錄啓動container
docker run -it -p 80:80 -v /usr/local/docker/nginx/conf:/etc/nginx \
-v /usr/local/docker/nginx/html:/usr/share/nginx/html \
-v /usr/local/docker/nginx/logs:/var/log/nginx \
--privileged=true --name mynginx nginx
 
 
nginx.conf (不變)
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
user  nginx;
worker_processes  auto;
 
error_log  /var/log/nginx/error.log notice;
pid        /var/run/nginx.pid;
 
 
events {
    worker_connections  1024;
}
 
 
http {
    include       /etc/nginx/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" $upstream_addr $upstream_response_time $request_time ';
 
    access_log  /var/log/nginx/access.log  main;
 
    sendfile        on;
    #tcp_nopush     on;
 
    keepalive_timeout  65;
 
    #gzip  on;
 
    include /etc/nginx/conf.d/*.conf;
}

 

default.conf
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
server {
    listen       80;
    listen  [::]:80;
    server_name  localhost;
 
    #access_log  /var/log/nginx/host.access.log  main;
 
    location / {
        root   /usr/share/nginx/html;
        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   /usr/share/nginx/html;
    }
     
    location /apptst/ {
        proxy_pass  http://192.168.0.109:8080/;
        proxy_set_header  Host              $http_host;   # required for docker client's sake
        proxy_set_header  X-Real-IP         $remote_addr; # pass on real client's IP
        proxy_set_header  X-Forwarded-For   $proxy_add_x_forwarded_for;
        proxy_set_header  X-Forwarded-Proto $scheme;
    }
}

  

4.改動了/usr/local/docker/nginx/conf/conf.d/default.conf后,要重啓container才生效
docker stop mynginx
docker start mynginx
測試
curl http://192.168.0.109/apptst/health-status
 
5.將app啓動在兩個端口上做負載均衡
default.conf
 
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
upstream apptst_server{
  server 192.168.0.109:8080 weight=1;
  server 192.168.0.109:8081 weight=1;
}
 
 
server {
    listen       80;
    listen  [::]:80;
    server_name  localhost;
 
    #access_log  /var/log/nginx/host.access.log  main;
 
    location / {
        root   /usr/share/nginx/html;
        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   /usr/share/nginx/html;
    }
     
    location /apptst/ {
        proxy_set_header  Host              $http_host;   # required for docker client's sake
        proxy_set_header  X-Real-IP         $remote_addr; # pass on real client's IP
        proxy_set_header  X-Forwarded-For   $proxy_add_x_forwarded_for;
        proxy_set_header  X-Forwarded-Proto $scheme;
       # proxy_pass http://192.168.0.109:8080/;
        proxy_pass http://apptst_server/;
    }
     
 
}

 

測試
curl http://192.168.0.109/apptst/health-status
=》
curl http://192.168.0.109:8080/health-status
curl http://192.168.0.109:8081/health-status
查看/usr/local/docker/nginx/logs/access.log

 

6.利用docker compose把nginx和兩個dotnet應用綁定在一起啓動

 

[centos@centos7 ~]$ cd /usr/app
[centos@centos7 app]$ docker-compose up -d
 
docker-compose.yml
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
version: '1.0'
 
services:
  nginx:
    restart: always
    image: nginx
    container_name: mynginx
    ports:
      - 80:80
      - 443:443
    volumes:
      - /usr/local/docker/nginx/conf:/etc/nginx
      - /usr/local/docker/nginx/html:/usr/share/nginx/html
      - /usr/local/docker/nginx/logs:/var/log/nginx
  apptst:
    restart: always
    image: apitest:v1
    container_name: apitest
    build:
      context: .
      dockerfile: /usr/app/apptst/Dockerfile
    ports:
      - 8080:80
  apptst1:
    restart: always
    image: apitest:v1
    container_name: apitest1
    build:
      context: .
      dockerfile: /usr/app/apptst/Dockerfile
    ports:
      - 8081:80

  

posted on   白马酒凉  阅读(372)  评论(0编辑  收藏  举报

编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
< 2025年3月 >
23 24 25 26 27 28 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 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示