1. 程式人生 > >【轉載】Docker部署nginx並修改配置文件

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

scrip pan eve 部署 keepaliv proxy format gin 一模一樣

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

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