服务器之家:专注于服务器技术及软件下载分享
分类导航

云服务器|WEB服务器|FTP服务器|邮件服务器|虚拟主机|服务器安全|DNS服务器|服务器知识|Nginx|IIS|Tomcat|

服务器之家 - 服务器技术 - 服务器知识 - Docker部署nginx并修改配置文件的实现方法

Docker部署nginx并修改配置文件的实现方法

2021-04-02 20:37wangfei0904306 服务器知识

这篇文章主要介绍了Docker部署nginx并修改配置文件的实现方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

docker 部署nginx,简直太简单了好吧

直接一行命令搞定:

?
1
2
3
4
5
docker run \
 --name nginx-health-web-pc \
 -d -p 6800:80 \
 -v /usr/docker/nginx/html:/usr/share/nginx/html \
 nginx

运行启动不亦乐乎~~~~~这时候忽然前端过来说:“你的nginx里得加一个配置”,顺带还告诉你:“某某某以前就是这样配的",

此时好胜的你当然不能拒绝,但是真正配置起来还是要费点心思的,一般情况下docker启动时进行配置,只要把配置文件的目录挂载出来就可以,简洁方便,但是nginx却是先加载一个主配置文件nginx.conf,在nginx.conf里再加载conf.d目录下的子配置文件(一般最少一个default.conf文件)。这比单独挂载一个目录麻烦了不少,但只要思路清晰,倒也不难。

我们先看挂载好的命令:

启动docker的命令

?
1
2
3
4
5
6
7
docker run \
 --name myNginx \
 -d -p 80:80 \
 -v /usr/docker/myNginx/html:/usr/share/nginx/html \
 -v /etc/docker/myNginx/nginx.conf:/etc/nginx/nginx.conf:ro \
 -v /etc/docker/myNginx/conf.d:/etc/nginx/conf.d \
 nginx

这里有几个注意事项:

(1)第一个“-v”,是项目位置,把项目放到挂载到的目录下即可;

(2)第二个“-v”,是挂载的主配置文件"nginx.conf",注意"nginx.conf"文件内有一行"include /etc/nginx/conf.d/*.conf;",这个include指向了子配置文件的路径,此处注意include后所跟的路径一定不要出错。

(3)第三个“-v”,把docker内子配置文件的路径也挂载了出来,注意要与(2)中include指向路径一致

(4)重点强调一下,nginx.conf是挂载了一个文件(docker是不推荐这样用的),conf.d挂载的是一个目录

我们先启动一下,可以发现是有问题的,因为配置文件还没有。

配置配置文件

我们找到常规方法安装的nginx时生成的配置文件(一般以“/etc/nginx”下),对应上面启动命令中的挂载位置,把主配置文件nginx.conf放到对应位置“/etc/docker/myNginx/nginx.conf”,把子配置文件“default.conf”放到“/etc/docker/myNginx/conf.d”目录下

重新运行启动命令,发现已经好了,至此docker中的文件已经可以随意配置,跟原生安装是一模一样的

思路:配置时一定要铆定一个思路:挂载出来的文件运行时是要加载到docker进程中去的!这样就不容易混淆。

---------------------------------------------------分隔线-------------------------------------------------------

贴出我的配置文件:

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
32
33
34
35
36
user root;
worker_processes 1;
 
error_log /var/log/nginx/error.log warn;
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"';
 
 access_log /var/log/nginx/access.log main;
 
 sendfile  on;
 #tcp_nopush  on;
 
 keepalive_timeout 65;
 
 autoindex on;
 
 #gzip on;
 
 include /etc/nginx/conf.d/*.conf;
 client_max_body_size 100M;
 client_header_buffer_size 128k;
 large_client_header_buffers 4 128k;
}

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
39
40
41
42
43
44
45
46
47
48
server {
 listen  80;
 server_name localhost;
 
 #charset koi8-r;
 #access_log /var/log/nginx/log/host.access.log main;
 
 location / {
  root /usr/nginx/dacheng-wechat-web;
  # root /usr/nginx/html;
  index index.html index.htm;
  autoindex on;
 try_files $uri /index/index/page.html;
  #try_files $uri /index/map/page.html;
 }
 
 #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;
 }
 
 # proxy the PHP scripts to Apache listening on 127.0.0.1:80
 #
 #location ~ \.php$ {
 # proxy_pass http://127.0.0.1;
 #}
 
 # 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;
 #}
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/wangfei0904306/article/details/77623400

延伸 · 阅读

精彩推荐