1.安装docker-compose
2.创建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
# 配置转发时可直接写 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中映射的文件夹
3.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;
#创建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;
}
}
自定义的一些转发配置文件例子: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;
}
4.配置生效重启,可写成reload.sh:
docker-compose exec web nginx -s reload
#注:exec 正在运行的容器中执行命令:nginx -s reload