網站首頁 編程語言 正文
1.從 docker 下載 Nginx 鏡像
docker pull nginx
2.創建掛載目錄
之后的文件就放這里面,對 docker 里 Nginx 對應的目錄進行映射,就不用改文件進到容器里了
mkdir -p /data/nginx/{conf,conf.d,html,logs}
3.為了保證文件的正確性,建議先進入容器把對應的文件給復制出來
不方便的可以開兩個窗口,一個進到容器里,左邊復制到右邊這樣,這是為了保證文件正確
#啟動容器
docker run -itd nginx /bin/bash
#進入容器
docker attach xxxxxxxxxx
說明 | 文件 | 掛載路徑 | nginx路徑 |
配置文件 | nginx.conf | /data/nginx/conf/nginx.conf | /etc/nginx/nginx.conf |
配置文件文件夾 | conf.d文件夾 | /data/nginx/conf.d | /etc/nginx/conf.d |
首頁文件夾html路徑 | html文件夾 | /data/nginx/html | /usr/share/nginx/html |
日志文件 | log文件夾 | /data/nginx/logs | /var/log/nginx |
這是對應的掛載目錄,把 nginx.conf 文件和 conf.d 里的 default.conf 復制到對應文件夾放好,后面就是修改了
4.接下來修改下 default.conf 文件就好了
這里我最多就改改端口號,訪問路徑之類的
server {
?
? ? #端口號
? ? listen ? ? ? 80;
? ? #定義使用 localhost 訪問
? ? server_name ?localhost;
?
? ? #charset koi8-r;
? ? #access_log ?/var/log/nginx/host.access.log ?main;
?
? ? location / {
? ? ? ? #根目錄位置
? ? ? ? root ? /usr/share/nginx/html;
? ? ? ? #index 文件位置
? ? ? ? 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;
? ? #}
}
這里測試用的 1.html 自己寫的
<html>
<head>
<title>Mynginx</title>
</head>
<body>
<h1>
歡迎使用nginx!
</h1>
</body>
</html>
5.接下來就可以啟動容器了
docker run --name myNginx -d -p 8089: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
掛載路徑一定要對好,別寫錯了
-p 8089:80 這里把 80 端口映射到主機的 8089 端口,這樣訪問就是 8089 端口了,不用去改 nginx 的默認端口
接下來就可以看下容器是否正常啟動
docker ps
要是沒有看到容器那說明啟動有問題,看看是配置文件寫的不對,還是掛載路徑不對之類的
啟動后就可以直接瀏覽器 localhost:8089 看到剛才寫的 1.index 頁面了
6.不停止 nginx 更新配置文件
當我們修改配置文件后要更新配置文件,這個時候開兩窗口就很爽
#進入容器
docker exec -it xxxxxxxxxxx /bin/bash
?
#測試配置文件是否有問題
nginx -t
?
#要是顯示 successful 就可以更新了
nginx -s reload
原文鏈接:https://blog.csdn.net/qq_37143673/article/details/88524610/
相關推薦
- 2024-07-18 【探索SpringCloud】服務發現-Nacos服務端數據結構和模型
- 2022-05-02 C++的輸入和輸出流詳解_C 語言
- 2022-07-19 C語言實現二叉樹的三種遍歷
- 2022-02-22 解決:DevTools failed to load SourceMap:... net::ERR_
- 2022-08-15 linux交叉編譯依賴包
- 2022-02-10 postgresql解決鎖表
- 2021-12-06 centos7.6批量增加修改刪除虛擬網卡操作介紹_Linux
- 2022-09-27 Android?JetPack組件的支持庫Databinding詳解_Android
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支