【轉載】Docker部署nginx並修改配置文件

docker 部署個nginx
docker run \
  --name nginx-health-web-pc \
  -d -p 6800:80 \
  -v /usr/docker/nginx/html:/usr/share/nginx/html \
  nginx

  

 

運行啓動不亦樂乎~~~~~這時候突然前端過來講:「你的nginx裏得加一個配置」,順帶還告訴你:「某某某之前就是這樣配的",php

此時好勝的你固然不能拒絕,可是真正配置起來仍是要費點心思的,通常狀況下docker啓動時進行配置,只要把配置文件的目錄掛載出來就能夠,簡潔方便,可是nginx倒是先加載一個主配置文件nginx.conf,在nginx.conf裏再加載conf.d目錄下的子配置文件(通常最少一個default.conf文件)。這比單獨掛載一個目錄麻煩了很多,但只要思路清晰,倒也不難。html

 

咱們先看掛載好的命令:前端

 啓動docker的命令nginx

 

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

  


這裏有幾個注意事項:web

 

(1)第一個「-v」,是項目位置,把項目放到掛載到的目錄下便可;docker

(2)第二個「-v」,是掛載的主配置文件"nginx.conf",注意"nginx.conf"文件內有一行"include /etc/nginx/conf.d/*.conf;",這個include指向了子配置文件的路徑,此處注意include後所跟的路徑必定不要出錯。bash

(3)第三個「-v」,把docker內子配置文件的路徑也掛載了出來,注意要與(2)中include指向路徑一致app

(4)重點強調一下,nginx.conf是掛載了一個文件(docker是不推薦這樣用的),conf.d掛載的是一個目錄tcp

 

咱們先啓動一下,能夠發現是有問題的,由於配置文件尚未。spa

配置配置文件

咱們找到常規方法安裝的nginx時生成的配置文件(通常以「/etc/nginx」下),對應上面啓動命令中的掛載位置,把主配置文件nginx.conf放到對應位置「/etc/docker/myNginx/nginx.conf」,把子配置文件「default.conf」放到「/etc/docker/myNginx/conf.d」目錄下

 

從新運行啓動命令,發現已經好了,至此docker中的文件已經能夠隨意配置,跟原生安裝是如出一轍的

 

思路:配置時必定要鉚定一個思路:掛載出來的文件運行時是要加載到docker進程中去的!這樣就不容易混淆。

 

---------------------------------------------------------------分隔線---------------------------------------------------------------------

貼出個人配置文件:

nginx.conf

 

 1 user  root;
 2 worker_processes  1;
 3  
 4 error_log  /var/log/nginx/error.log warn;
 5 pid        /var/run/nginx.pid;
 6  
 7  
 8 events {
 9     worker_connections  1024;
10 }
11  
12  
13 http {
14     include       /etc/nginx/mime.types;
15     default_type  application/octet-stream;
16  
17     log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
18                       '$status $body_bytes_sent "$http_referer" '
19                       '"$http_user_agent" "$http_x_forwarded_for"';
20  
21     access_log  /var/log/nginx/access.log  main;
22  
23     sendfile        on;
24     #tcp_nopush     on;
25  
26     keepalive_timeout  65;
27  
28     autoindex  on;
29     
30     #gzip  on;
31  
32     include /etc/nginx/conf.d/*.conf;
33  
34     client_max_body_size 100M;
35  
36     client_header_buffer_size    128k;
37     large_client_header_buffers  4  128k;
38 }

 

default.conf

 

 

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

相關文章
相關標籤/搜索