天天看點

【轉載】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裡得加一個配置”,順帶還告訴你:“某某某以前就是這樣配的",

此時好勝的你當然不能拒絕,但是真正配置起來還是要費點心思的,一般情況下docker啟動時進行配置,隻要把配置檔案的目錄挂載出來就可以,簡潔友善,但是nginx卻是先加載一個主配置檔案nginx.conf,在nginx.conf裡再加載conf.d目錄下的子配置檔案(一般最少一個default.conf檔案)。這比單獨挂載一個目錄麻煩了不少,但隻要思路清晰,倒也不難。

我們先看挂載好的指令:

 啟動docker的指令

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 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