使⽤Docker安装Nginx并配置端⼝转发使⽤docker安装并运⾏nginx命令:
docker run --name=nginx -p 80:80 -d docker.io/nginx
使⽤命令:
docker exec -it nginx /bin/bash 进⼊容器可查看到⼏个重要的⽂件
配置⽂件:f 在 /etc/f
⽇志⽂件: /var/log/nginx/access.log /var/log/nginx/error.log
使⽤cat命令打开f
1 root@dc048fc59765:/var/log/nginx# cat /etc/f
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/pes;
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    include /etc/nginx/conf.d/*.conf;
33}
34root@dc048fc59765:/var/log/nginx#
发现第32⾏,配置了⼀个⼦配置⽂件,进⼊conf.d发现有⼀个f⽂件
打开f⽂件:
1 root@dc048fc59765:/etc/nginx/conf.d# f
2 server {
3    listen      80;
4    listen  [::]:80;
5    server_name  localhost;
6
7    #charset koi8-r;
8    #access_log  /var/log/nginx/host.access.log  main;
9
10    location / {
11        root  /usr/share/nginx/html;
12        index  index.html index.htm;
13    }
14
15    #error_page  404              /404.html;
16
17    # redirect server error pages to the static page /50x.html
18    #
19    error_page  500502503504  /50x.html;
20    location = /50x.html {
21        root  /usr/share/nginx/html;
22    }
23
24    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
25    #
26    #location ~ \.php$ {
27    #    proxy_pass  127.0.0.1;
28    #}
29
30    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
31    #
32    #location ~ \.php$ {
33    #    root          html;
34    #    fastcgi_pass  127.0.0.1:9000;
35    #    fastcgi_index  index.php;
36    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
37    #    include        fastcgi_params;
38    #}
39
40    # deny access to .htaccess files, if Apache's document root
41    # concurs with nginx's one
42    #
43    #location ~ /\.ht {
44    #    deny  all;
45    #}
46 }
47
48 root@dc048fc59765:/etc/nginx/conf.d#
从f中11⾏可以看出,index页⾯在/usr/share/nginx/html
现在,我们配置⼀下容器卷:
docker rm -f nginx 删除⼀下之前的容器
再次执⾏⽐较完整的命令:
1 docker run \
2  --restart=always \
3  --name nginx \
4  -d -p 80:80 \
5  -v /data/nginx/html:/usr/share/nginx/html \
6  -v /data/f:/etc/f \
7  -v /data/nginx/conf.d:/etc/nginx/conf.d \
8  -v /data/nginx/log:/var/log/nginx \
9  nginx
这⾥有⼏个注意事项:
(1)第⼀个“-v”,是项⽬位置,把html代码放到挂载到的⽬录下即可;
(2)第⼆个“-v”,是挂载的主配置⽂件"f",注意"f"⽂件内有⼀⾏"include /etc/nginx/conf.d/*.conf;",
这个include指向了⼦配置⽂件的路径,此处注意include后所跟的路径⼀定不要出错。
(3)第三个“-v”,把docker内⼦配置⽂件的路径也挂载了出来,注意要与(2)中include指向路径⼀致
(4)重点强调⼀下,f是挂载了⼀个⽂件(docker是不推荐这样⽤的),conf.d挂载的是⼀个⽬录
我们先启动⼀下,可以发现是有问题的,因为配置⽂件还没有。
[root@zuul-server data]# docker run  --name nginx  -d -p 80:80  -v /data/nginx/html:/usr/share/nginx/html  -v /data/f:/etc/f  -v /data/nginx/conf.d:/etc/nginx/conf.d  -v /data/nginx/log:/var/log/nginx  nginx
c8d49810b4afd4b6661beb942f0f19a67cf64f9798af9d2eb8a2aa242b2af434
docker: Error response from daemon: OCI runtime create failed: :349: starting container process caused ":449: container init caused \":58: mounting \\\"/data/f\\\" to rootfs \\\"/var/li   
很明显,报错了,容器也没有启动成功,怎么解决了?
解决办法:
1 进⼊/data/nginx⽬录,
2 删除f⽂件夹  rm  -f
3 新建f⽂件    f
4 然后把之前cat /etc/f的内容放⼊到f
5 同理,cd /data/nginx/conf.d ,f,把之前cat /etc/nginx/conf.f中的内容放⼊到新建的f中。docker进入容器
最后 docker restart nginx 搞定。
1 server {
2  listen  80;
3  server_name localhost;
4
5  #charset koi8-r;
6  #access_log /var/log/nginx/log/host.access.log main;
7
8  location / {
9  #root /usr/nginx/dacheng-wechat-web;
10  #root /usr/nginx/html;
11  root /usr/share/nginx/html;
12  index index.html index.htm;
13  autoindex on;
14  # try_files $uri /index/index/page.html;
15  # try_files $uri /index/map/page.html;
16  }
17
18 location /mp {
19  proxy_pass 192.168.11.241:20001/;
20
21 }
22
23
24  #error_page 404    /404.html;
25
26  # redirect server error pages to the static page /50x.html
27  #
28  error_page 500502503504 /50x.html;
29  location = /50x.html {
30  root /usr/share/nginx/html;
31  }
32
33  # proxy the PHP scripts to Apache listening on 127.0.0.1:80
34  #
35  #location ~ \.php$ {
36  # proxy_pass 127.0.0.1;
37  #}
38
39  # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
40  #
41  #location ~ \.php$ {
42  # root  html;
43  # fastcgi_pass 127.0.0.1:9000;
44  # fastcgi_index index.php;
45  # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
46  # include  fastcgi_params;
47  #}
48
49  # deny access to .htaccess files, if Apache's document root
50  # concurs with nginx's one
51  #
52  #location ~ /\.ht {
53  # deny all;
54  #}
55 }

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。