1. 程式人生 > >使用docker安裝nginx並配置埠轉發

使用docker安裝nginx並配置埠轉發

使用docker安裝並執行nginx命令:

 docker run --name=nginx -p 80:80 -d docker.io/nginx

 

使用命令:

docker exec -it nginx /bin/bash 進入容器可檢視到幾個重要的檔案

 

配置檔案:nginx.conf 在 /etc/nginx/nginx.conf

日誌檔案: /var/log/nginx/access.log /var/log/nginx/error.log

 

使用cat命令開啟nginx.conf

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

發現第32行,配置了一個子配置檔案,進入conf.d發現有一個default.conf檔案

開啟default.conf檔案:

 1 root@dc048fc59765:/etc/nginx/conf.d# cat default.conf 
 2 server {
 3     listen       80;
 4     listen  [::]:80;
 5     server_name  localhost;
 6 
 7     #charset koi8-r;
 8     #access_log  /var/log/nginx/host.access.log  main;
 9 
10     location / {
11         root   /usr/share/nginx/html;
12         index  index.html index.htm;
13     }
14 
15     #error_page  404              /404.html;
16 
17     # redirect server error pages to the static page /50x.html
18     #
19     error_page   500 502 503 504  /50x.html;
20     location = /50x.html {
21         root   /usr/share/nginx/html;
22     }
23 
24     # proxy the PHP scripts to Apache listening on 127.0.0.1:80
25     #
26     #location ~ \.php$ {
27     #    proxy_pass   http://127.0.0.1;
28     #}
29 
30     # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
31     #
32     #location ~ \.php$ {
33     #    root           html;
34     #    fastcgi_pass   127.0.0.1:9000;
35     #    fastcgi_index  index.php;
36     #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
37     #    include        fastcgi_params;
38     #}
39 
40     # deny access to .htaccess files, if Apache's document root
41     # concurs with nginx's one
42     #
43     #location ~ /\.ht {
44     #    deny  all;
45     #}
46 }
47 
48 root@dc048fc59765:/etc/nginx/conf.d# 

在瀏覽器中輸入:http://192.168.11.241

 從default.conf中11行可以看出,index頁面在/usr/share/nginx/html

 

現在,我們配置一下容器卷:

docker rm -f nginx 刪除一下之前的容器

再次執行比較完整的命令:

1 docker run \
2  --restart=always \
3  --name nginx \
4  -d -p 80:80 \
5  -v /data/nginx/html:/usr/share/nginx/html \
6  -v /data/nginx/nginx.conf:/etc/nginx/nginx.conf \
7  -v /data/nginx/conf.d:/etc/nginx/conf.d \
8  -v /data/nginx/log:/var/log/nginx \
9  nginx

這裡有幾個注意事項:

(1)第一個“-v”,是專案位置,把html程式碼放到掛載到的目錄下即可;

(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掛載的是一個目錄

我們先啟動一下,可以發現是有問題的,因為配置檔案還沒有。

[root@zuul-server data]# docker run  --name nginx  -d -p 80:80  -v /data/nginx/html:/usr/share/nginx/html  -v /data/nginx/nginx.conf:/etc/nginx/nginx.conf  -v /data/nginx/conf.d:/etc/nginx/conf.d  -v /data/nginx/log:/var/log/nginx  nginx
c8d49810b4afd4b6661beb942f0f19a67cf64f9798af9d2eb8a2aa242b2af434
docker: Error response from daemon: OCI runtime create failed: container_linux.go:349: starting container process caused "process_linux.go:449: container init caused \"rootfs_linux.go:58: mounting \\\"/data/nginx/nginx.conf\\\" to rootfs \\\"/var/lib/docker/overlay2/ee154ee69264707a542409b514cfff950b31cefa4dcd4e66c3635d0aa94f5058/merged\\\" at \\\"/var/lib/docker/overlay2/ee154ee69264707a542409b514cfff950b31cefa4dcd4e66c3635d0aa94f5058/merged/etc/nginx/nginx.conf\\\" caused \\\"not a directory\\\"\"": unknown: Are you trying to mount a directory onto a file (or vice-versa)? Check if the specified host path exists and is the expected type.

  

很明顯,報錯了,容器也沒有啟動成功,怎麼解決了?

解決辦法:

1 進入/data/nginx目錄,

2 刪除nginx.conf資料夾  rm  -rf nginx.conf

3 新建nginx.conf檔案    touch nginx.conf

4 然後把之前cat /etc/nginx/nginx.conf的內容放入到nginx.conf

5 同理,cd /data/nginx/conf.d ,touch default.conf,把之前 cat /etc/nginx/conf.d/default.conf中的內容放入到新建的default.conf中。

 

最後 docker restart nginx 搞定。

 

 

 需要配置一個埠轉發功能,業務需求是:以http://192.168.11/241/mp開頭的請求需要重定向到http://192.168.11.241:20001/mp上,

剛開始把 proxy_pass 對應的路徑寫成了 http://127.0.0.1:20001/;導致報404錯誤,原因很簡單,nginx運作在容器裡面,肯定找不到http://127.0.0.1:20001/,浪費了我一點點時間,一時沒轉過彎來。

 1 server {
 2  listen  80;
 3  server_name localhost;
 4   
 5  #charset koi8-r;
 6  #access_log /var/log/nginx/log/host.access.log main;
 7   
 8  location / {
 9   #root /usr/nginx/dacheng-wechat-web;
10   #root /usr/nginx/html;
11   root /usr/share/nginx/html;
12   index index.html index.htm;
13   autoindex on;
14   # try_files $uri /index/index/page.html;
15   # try_files $uri /index/map/page.html;
16  }
17 
18 location /mp {
19   proxy_pass http://192.168.11.241:20001/;
20 
21 }
22 
23   
24  #error_page 404    /404.html;
25   
26  # redirect server error pages to the static page /50x.html
27  #
28  error_page 500 502 503 504 /50x.html;
29  location = /50x.html {
30   root /usr/share/nginx/html;
31  }
32   
33  # proxy the PHP scripts to Apache listening on 127.0.0.1:80
34  #
35  #location ~ \.php$ {
36  # proxy_pass http://127.0.0.1;
37  #}
38   
39  # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
40  #
41  #location ~ \.php$ {
42  # root   html;
43  # fastcgi_pass 127.0.0.1:9000;
44  # fastcgi_index index.php;
45  # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
46  # include  fastcgi_params;
47  #}
48   
49  # deny access to .htaccess files, if Apache's document root
50  # concurs with nginx's one
51  #
52  #location ~ /\.ht {
53  # deny all;
54  #}
55 }

 

 

 

 

 

 

 

 

 

 

 

 

 

 

&n