日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

docker 安装nginx 配置目录挂载

發布時間:2025/3/21 编程问答 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 docker 安装nginx 配置目录挂载 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

目錄
nginx 的安裝
運行mynginx容器
設置開機自動啟動容器
nginx 的安裝
mkdir /usr/local/docker
cd /usr/local/docker
docker run --name nginxtest -d nginx #運行一個測試的nginx
docker cp nginxtest:/etc/nginx ./ #把容器里的nginx的目錄復制
docker rm -f nginxtest #刪除測試的nginx-test
復制出來的nginx目錄結構

#復制出來的nginx目錄結構
[root@localhost docker]# tree nginx
nginx
├── conf.d
│ └── default.conf
├── fastcgi_params
├── koi-utf
├── koi-win
├── logs
│ ├── access.log
│ ├── error.log
│ ├── gin.haimait.com.access.log
│ ├── gin.haimait.com.error.log
│ ├── localhost.access.log
│ └── localhost.error.log
├── mime.types
├── modules -> /usr/lib/nginx/modules
├── nginx.conf
├── scgi_params
├── uwsgi_params
└── win-utf
運行mynginx容器
#運行mynginx 掛載到/wwwroot是為了保持容器和宿主機目錄一致,以免配置時出錯

docker run
–name mynginx
-p 8081:80
-p 443:443
-v /etc/localtime:/etc/localtime:ro
-v /wwwroot:/wwwroot
-v $PWD/nginx:/etc/nginx
-v $PWD/nginx/logs:/var/log/nginx
–restart always
-d nginx

編寫默認的配置文件

vim $PWD/nginx/conf.d/default.conf

把內容換下面的內容

server {

listen 80;server_name localhost;access_log /var/log/nginx/localhost.access.log; error_log /var/log/nginx/localhost.error.log;# 配置前端靜態文件目錄location / {root /wwwroot/html;index index.html index.htm index.php; }#location ~ \.php$ { # fastcgi_pass myphp73-fpm:9000; #myphp73-fpm容器的名字 # fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; # include fastcgi_params; #}# 配置后臺go服務api接口服務 代理到8877端口 #location ~ ^/goadminapi/ { # 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_set_header X-Forwarded-Proto $scheme; # rewrite ^/goadminapi/(.*)$ /$1 break; # proxy_pass http://127.0.0.1:8877; # }

}

配置一個域名也指向到/wwwroot/html目錄 確定你的域名已經解析到服務器的ip上了

vim $PWD/nginx/conf.d/gin.haimait.com.conf

內容如下:

server {

listen 80;server_name gin.haimait.com;access_log /var/log/nginx/gin.haimait.com.access.log; error_log /var/log/nginx/gin.haimait.com.error.log;# 配置前端靜態文件目錄 location / {root /wwwroot/html; index index.html index.htm index.php; }error_page 500 502 503 504 /50x.html; location = /50x.html {root /usr/share/nginx/html; }#location ~ \.php$ { # fastcgi_pass myphp73-fpm:9000; #myphp73-fpm容器的名字 # fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; # include fastcgi_params; #}# 配置后臺go服務api接口服務 代理到8877端口 #location ~ ^/goadminapi/ { # 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_set_header X-Forwarded-Proto $scheme; # rewrite ^/goadminapi/(.*)$ /$1 break; # proxy_pass http://127.0.0.1:8877; # }

}
重啟nginx

docker restart mynginx

新建index.html

mkdir /wwwroot/html
vim /wwwroot/html/index.html
內容如下:

hello nginx , html

測試

以下說明都已經配置成功了

[root@localhost ~]# curl localhost:8081
hello nginx , html
[root@localhost ~]# curl gin.haimait.com:8081
hello nginx , html
瀏覽器訪問

刪除nginx容器重啟容器,wwwroot里的內容也不會丟

到此已經完成安裝

設置開機自動啟動容器
在運行docker容器時可以加如下參數來保證每次docker服務重啟后容器也自動重啟:

docker run --restart=always CONTAINER ID

目錄
nginx 的安裝
運行mynginx容器
設置開機自動啟動容器
nginx 的安裝
mkdir /usr/local/docker
cd /usr/local/docker
docker run --name nginxtest -d nginx #運行一個測試的nginx
docker cp nginxtest:/etc/nginx ./ #把容器里的nginx的目錄復制
docker rm -f nginxtest #刪除測試的nginx-test
復制出來的nginx目錄結構

#復制出來的nginx目錄結構
[root@localhost docker]# tree nginx
nginx
├── conf.d
│ └── default.conf
├── fastcgi_params
├── koi-utf
├── koi-win
├── logs
│ ├── access.log
│ ├── error.log
│ ├── gin.haimait.com.access.log
│ ├── gin.haimait.com.error.log
│ ├── localhost.access.log
│ └── localhost.error.log
├── mime.types
├── modules -> /usr/lib/nginx/modules
├── nginx.conf
├── scgi_params
├── uwsgi_params
└── win-utf
運行mynginx容器
#運行mynginx 掛載到/wwwroot是為了保持容器和宿主機目錄一致,以免配置時出錯

docker run
–name mynginx
-p 8081:80
-p 443:443
-v /etc/localtime:/etc/localtime:ro
-v /wwwroot:/wwwroot
-v $PWD/nginx:/etc/nginx
-v $PWD/nginx/logs:/var/log/nginx
–restart always
-d nginx

編寫默認的配置文件

vim $PWD/nginx/conf.d/default.conf

把內容換下面的內容

server {

listen 80;server_name localhost;access_log /var/log/nginx/localhost.access.log; error_log /var/log/nginx/localhost.error.log;# 配置前端靜態文件目錄location / {root /wwwroot/html;index index.html index.htm index.php; }#location ~ \.php$ { # fastcgi_pass myphp73-fpm:9000; #myphp73-fpm容器的名字 # fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; # include fastcgi_params; #}# 配置后臺go服務api接口服務 代理到8877端口 #location ~ ^/goadminapi/ { # 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_set_header X-Forwarded-Proto $scheme; # rewrite ^/goadminapi/(.*)$ /$1 break; # proxy_pass http://127.0.0.1:8877; # }

}

配置一個域名也指向到/wwwroot/html目錄 確定你的域名已經解析到服務器的ip上了

vim $PWD/nginx/conf.d/gin.haimait.com.conf

內容如下:

server {

listen 80;server_name gin.haimait.com;access_log /var/log/nginx/gin.haimait.com.access.log; error_log /var/log/nginx/gin.haimait.com.error.log;# 配置前端靜態文件目錄 location / {root /wwwroot/html; index index.html index.htm index.php; }error_page 500 502 503 504 /50x.html; location = /50x.html {root /usr/share/nginx/html; }#location ~ \.php$ { # fastcgi_pass myphp73-fpm:9000; #myphp73-fpm容器的名字 # fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; # include fastcgi_params; #}# 配置后臺go服務api接口服務 代理到8877端口 #location ~ ^/goadminapi/ { # 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_set_header X-Forwarded-Proto $scheme; # rewrite ^/goadminapi/(.*)$ /$1 break; # proxy_pass http://127.0.0.1:8877; # }

}
重啟nginx

docker restart mynginx

新建index.html

mkdir /wwwroot/html
vim /wwwroot/html/index.html
內容如下:

hello nginx , html

測試

以下說明都已經配置成功了

[root@localhost ~]# curl localhost:8081
hello nginx , html
[root@localhost ~]# curl gin.haimait.com:8081
hello nginx , html
瀏覽器訪問

刪除nginx容器重啟容器,wwwroot里的內容也不會丟

到此已經完成安裝

設置開機自動啟動容器
在運行docker容器時可以加如下參數來保證每次docker服務重啟后容器也自動重啟:

docker run --restart=always CONTAINER ID

如果已經啟動了則可以使用如下命令:

docker update --restart=always CONTAINER ID #設置啟動nginx時自動啟動
docker update --restart=no CONTAINER ID #設置啟動nginx時不啟動

[root@localhost www]# docker update --restart=always nginx
nginx

總結

以上是生活随笔為你收集整理的docker 安装nginx 配置目录挂载的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。