1. 安裝 Docker-compose
curl -L "https://github.com/docker/compose/releases/download/v2.17.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose
ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose
2.拉取映象
docker pull registry.cn-hangzhou.aliyuncs.com/ns-2023/lib-2023:nginx-1.21.5
3. 配置檔案 ./nginx/conf/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;
server {
listen 80;
server_name localhost; # 伺服器地址或繫結域名
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
# =========================================================
# ================== ↓↓↓↓↓↓ start ↓↓↓↓↓↓ ==================
# =========================================================
location / {
root /usr/share/nginx/html;
#try_files $uri $uri/ @router;
index index.html index.htm;
try_files $uri $uri/ /index.html; # 解決頁面重新整理 404 問題
#proxy_pass http://zhengqingya.gitee.io; # 代理的ip地址和埠號
#proxy_connect_timeout 600; #代理的連線超時時間(單位:毫秒)
#proxy_read_timeout 600; #代理的讀取資源超時時間(單位:毫秒)
}
#location @router {
#rewrite ^.*$ /index.html last; # 攔截80埠後的所有請求地址到登入頁面 -> 相當於後端的攔截器
#}
# location ^~ /api { # ^~/api/表示匹配字首為api的請求
# proxy_pass http://www.zhengqingya.com:5000/api/; # 注:proxy_pass的結尾有/, -> 效果:會在請求時將/api/*後面的路徑直接拼接到後面
#
# # proxy_set_header作用:設定傳送到後端伺服器(上面proxy_pass)的請求頭值
# # 【當Host設定為 $http_host 時,則不改變請求頭的值;
# # 當Host設定為 $proxy_host 時,則會重新設定請求頭中的Host資訊;
# # 當為$host變數時,它的值在請求包含Host請求頭時為Host欄位的值,在請求未攜帶Host請求頭時為虛擬主機的主域名;
# # 當為$host:$proxy_port時,即攜帶埠傳送 ex: $host:8080 】
# proxy_set_header Host $host;
#
# proxy_set_header X-Real-IP $remote_addr; # 在web伺服器端獲得使用者的真實ip 需配置條件① 【 $remote_addr值 = 使用者ip 】
# proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;# 在web伺服器端獲得使用者的真實ip 需配置條件②
# proxy_set_header REMOTE-HOST $remote_addr;
# # proxy_set_header X-Forwarded-For $http_x_forwarded_for; # $http_x_forwarded_for變數 = X-Forwarded-For變數
# }
# location ^~ /blog/ {
# proxy_pass http://zhengqingya.gitee.io/blog/; # ^~/blog/表示匹配字首是blog的請求,proxy_pass的結尾有/, 則會把/blog/*後面的路徑直接拼接到後面,即移除blog
#
# proxy_set_header Host $proxy_host; # 改變請求頭值 -> 轉發到碼雲才會成功
# proxy_set_header X-Real-IP $remote_addr;
# proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# proxy_set_header X-NginX-Proxy true;
# }
# =========================================================
# ================== ↑↑↑↑↑↑ end ↑↑↑↑↑↑ ==================
# =========================================================
#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;
}
}
}
主配置檔案 /nginx/conf/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 index.html index.htm;
}
#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;
#}
}
前端錯誤頁面 ./nginx/html/50x.html
<!DOCTYPE html>
<html>
<head>
<title>Error</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>An error occurred.</h1>
<p>
Sorry, the page you are looking for is currently unavailable.<br />
Please try again later.
</p>
<p>
If you are the system administrator of this resource then you should check
the error log for details.
</p>
<p><em>Faithfully yours, nginx.</em></p>
</body>
</html>
前端預設頁
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>
If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.
</p>
<p>
For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br />
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.
</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
4. docker-compose.yml
version: "3"
services:
nginx:
image: registry.cn-hangzhou.aliyuncs.com/ns-2023/lib-2023:nginx-1.21.5
container_name: nginx # 容器名為'nginx'
restart: unless-stopped # 指定容器退出後的重啟策略為始終重啟,但是不考慮在Docker守護程序啟動時就已經停止了的容器
volumes: # 資料卷掛載路徑設定,將本機目錄對映到容器目錄
- "./nginx/conf/nginx.conf:/etc/nginx/nginx.conf"
- "./nginx/conf/conf.d/default.conf:/etc/nginx/conf.d/default.conf"
- "./nginx/html:/usr/share/nginx/html"
- "./nginx/log:/var/log/nginx"
environment: # 設定環境變數,相當於docker run命令中的-e
TZ: Asia/Shanghai
LANG: en_US.UTF-8
ports: # 對映埠
- "80:80"
5. 執行
# 執行
docker-compose -f docker-compose-nginx.yml -p nginx up -d
# 進入容器
docker exec -it nginx /bin/bash
# nginx修改配置後過載
nginx -s reload