1. 程式人生 > >Docker部署nginx並修改配置檔案

Docker部署nginx並修改配置檔案

來源:https://blog.csdn.net/wangfei0904306/article/details/77623400

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

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
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;
    #}
}