docker-compose部署nginx教程
更新時間:2025年01月04日 10:01:37 作者:與民更始
文章介紹了如何安裝和配置docker-compose,創(chuàng)建一個Nginx容器,并通過docker-compose.yml文件進行配置,包括映射文件夾和自定義轉發(fā)配置,最后,提供了重啟和配置生效的腳本
docker-compose部署nginx
安裝docker-compose
創(chuàng)建nginx文件夾
添加docker-compose.yml文件:
version: '3.3'
services:
web:
image: "xtulnx/nginx:tengine-latest"
container_name: nginx
hostname: s.nginx
volumes:
- ./conf.d:/etc/nginx/conf.d
- ./html:/etc/nginx/html
- ./data:/data
- ./logs:/etc/nginx/logs
# 配置轉發(fā)時可直接寫 proxy:s.dev
extra_hosts:
- "s.host:170.170.0.1"
- "s.dev:127.0.0.1"
working_dir: /etc/nginx
ports:
- "80:80"
- "443:443"
environment:
- NGINX_PORT=80
restart: always
在當前文件下添加volumes中映射的文件夾
conf.d文件下的default.conf
log_format custom_log '$remote_addr - $remote_user [$time_local] '
'"$request" $status $body_bytes_sent '
'"$http_referer" "$http_user_agent" '
'$request_body $query_string '
'"$http_user_agent" "$http_x_forwarded_for" "$request_uri" '
'proxy_to: $upstream_addr';
log_format httplog '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" $request_body $query_string'
'"$http_user_agent" "$http_x_forwarded_for" "$request_uri"';
log_format uplg '$remote_addr - $remote_user [$time_local] [$upstream_addr] "$request" [$request_body]'
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
log_format hu '$remote_addr - $remote_user [$time_local] "$request" '
'"$status" $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for" '
'"$gzip_ratio" $request_time $bytes_sent $request_length' ' $request_body';
access_log logs/access.log custom_log;
# sendfile on;
#增加一下websocket配置
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
server {
listen 80;
listen [::]:80;
server_name localhost;
#access_log /var/log/nginx/host.access.log main;
#創(chuàng)建de
include conf.d/*.conf;
location / {
# root /usr/share/nginx/html;
root /etc/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;
}
}自定義的一些轉發(fā)配置文件例子:
- user.conf
#重寫路徑的例子
#rewrite ^/dev_user/(.*\.*)$ /user/$1;
location ^~/user/ {
#s.dev是nginx的compose里面配置的
proxy_pass http://s.dev:8080/user/;
#proxy_pass http://10.22.22.22:8080/user/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 100m;
}配置生效重啟
- 可寫成reload.sh:
docker-compose exec web nginx -s reload #注:exec 正在運行的容器中執(zhí)行命令:nginx -s reload
總結
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
基于alpine用dockerfile創(chuàng)建的爬蟲Scrapy鏡像的實現(xiàn)
這篇文章主要介紹了基于alpine用dockerfile創(chuàng)建的爬蟲Scrapy鏡像的實現(xiàn),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-12-12
docker快速搭建私有鏡像倉庫registry以及registryUI方式
本文詳細介紹了如何在Windows系統(tǒng)上使用Docker搭建私有鏡像倉庫,并通過registry-web界面進行管理,內容包括環(huán)境準備、搭建步驟及上傳和下載鏡像的操作2024-12-12
docker中使用mongodb數(shù)據庫詳解(在局域網訪問)
這篇文章主要給大家介紹了在docker中使用mongodb數(shù)據庫,在局域網訪問的相關資料,文中將步驟介紹的非常詳細,對大家具有一定的參考學習價值,需要的朋友們下面來一起看看吧。2017-06-06
docker中使用mysql數(shù)據庫詳解(在局域網訪問)
這篇文章主要給大家介紹了在docker中使用mysql數(shù)據庫,在局域網訪問的相關資料,文中通過圖文以及示例代碼介紹的非常詳細,對大家具有一定的參考學習價值,需要的朋友們下面來一起看看吧。2017-06-06
docker?registry刪除遠程倉庫鏡像實現(xiàn)方式
文章介紹如何清理Docker?Registry中堆積的鏡像,通過配置刪除功能、啟動容器、查看鏡像信息并執(zhí)行刪除操作,同時提供基于web-ui的管理方案,優(yōu)化存儲空間使用2025-09-09

