1. 程式人生 > >用 Docker 執行 Nginx 容器做域名反向代理

用 Docker 執行 Nginx 容器做域名反向代理

這段時間在研究docker,在部署nginx時遇到了坑,最主要的問題是在掛載檔案和資料夾的時候不知道怎麼掛載,經過反覆實驗以及檢視網上的教程,先總結如下:

1首先pull下載nginx映象包

docker pull nginx:5.7

2(關鍵)檢視nginx映象裡面配置檔案、日誌等檔案的具體位置,只有找到映象配置檔案的路徑,後面掛載檔案和資料夾才能覆蓋這些路徑

以終端的方式開啟映象容器

docker run -i -t nginx /bin/bash

找到映象中nginx.conf配置檔案路徑/etc/nginx/nginx.conf

用命令:cat  /etc/nginx/nginx.conf 檢視nginx.conf檔案內容然後拷貝出來

找到default.conf配置檔案的路徑/etc/nginx/conf.d/default.conf

用命令:cat  /etc/nginx/conf.d/default.conf 檢視default.conf檔案內容然後拷貝出來

找到預設首頁資料夾html路徑/usr/share/nginx/html

找到日誌檔案路徑/var/log/nginx

然後輸入exit退出容器的終端

3用nginx映象啟動容器mynginx並且掛載資料夾和檔案到容器中

這裡說明一下為什麼我要掛載配置檔案和資料夾,如果你部署應用並且很輕易地修改nginx的配置檔案,如果掛載了檔案或者資料夾那麼你只需要修改掛載源的檔案或者資料夾裡面的檔案就可以了,而不用每次都要使用docker run -i -t nginx /bin/bash命令進入到映象終端中去修改配置檔案,下面我將演示修改自己的nginx首頁,並且將其掛載上去容器中覆蓋掉原來的預設的首頁

在linux系統中建立掛載原始檔和資料夾(我的是centos7)

mkdir -p /data/nginx/conf
mkdir -p /data/nginx/conf.d
mkdir -p /data/nginx/html
mkdir -p /data/nginx/logs

然後建立在conf資料夾裡面建立一個nginx.conf配置檔案,並且輸入一下內容,建議大家不要照抄我的配置,用我上面介紹的第一步的方法進入到nginx容器的終端中複製nginx.conf配置檔案的內容到linux系統中這個新建立的nginx.conf檔案中進行修改,這樣子就保證了配置檔案中的路徑與映象中配置檔案的路徑能保持一致

[
[email protected]
/]# cd /data/nginx/conf [[email protected] conf]# more nginx.conf user nginx; 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; #gzip on; include /etc/nginx/conf.d/*.conf; }

在conf.d裡面建立一個default.conf檔案,並且輸入一下內容,同樣這個內容也是我從映象中default.conf預設的配置檔案中複製過來修改的,同樣建議大家不要照抄我的內容,因為涉及到路徑那些可能會與你們nginx映象中的路徑不一致,這樣子在啟動映象建立容器的時候就無法用掛載的方法覆蓋掉容器中的路徑

[[email protected] conf]#  more /data/nginx/conf.d/default.conf   
server {
    listen       80;
    server_name  localhost;
 
    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;
 
    location / {
        root   /usr/share/nginx/html;
        index 1.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;
    #}
}

大家注意了,這裡我修改了一下原來預設配置檔案裡面的內容,在上面的其中一個location中,我把nginx預設首頁index改成了1.html,1.html是我自己建立的首頁名

在html資料夾下建立1.html首頁檔案,並且編寫屬於自己的首頁,這裡我是用notepadd++在windows上面寫好了1.html檔案再通過工具拷過去linux系統裡面的,注意有中文的可能要轉換下編碼,不然可能會亂碼,例如我這裡用的是ansi的編碼

<html>
<head>
<title>Mynginx</title>
</head>
<body>
<h1>
歡迎使用nginx!
</h1>
</body>
</html>

配置反向代理

在conf.d裡面用 vi www.ygksfp.com.conf 命令建立域名配置檔案並輸入以下內容:

server {
    listen  80;
    server_name  www.if404.com;
    access_log /var/log/nginx/if404.access.log main;
    error_log /var/log/nginx/if404.error.log error;
    location / {
        proxy_set_header  Host  $http_host;
        proxy_set_header  X-Real-IP  $remote_addr;
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_pass  http://宿主機IP:8080;
    }
}

現在是建立容器並且掛載檔案和檔案夾了

docker run  --name mynginx -d -p 80:80 -v /data/nginx/html:/usr/share/nginx/html -v /data/nginx/conf/nginx.conf:/etc/nginx/nginx.conf -v /data/nginx/conf.d:/etc/nginx/conf.d  -v /data/nginx/logs:/var/log/nginx nginx

記住掛載的目標目錄或者檔案路徑要與映象中的路徑保持一致如/etc/nginx/nginx.conf,這個路徑在第二步裡面已經找出來了

docker ps 檢視有沒有啟動成功

如果沒有啟動成功要先用docker ps -a檢視失敗的容器,並且用docker rm CONTAILNER ID刪除容器ID,再查詢問題,然後docker run再啟動容器,如果在確保掛載的目錄和檔案沒有問題還是不能啟動的話,那麼就是許可權問題了,網上說的就是在docker run後面加個 --privileged=true引數

http://IP  開啟網頁看看效果把

輸入域名看域名反向代理是否成功

我的域名還沒備案開啟暫時是這個效果,反向代理是成功了的

感謝二位大神的詳細文章: