Docker常用容器镜像
docker pull registry.cn-shenzhen.aliyuncs.com/liyang_main/java_environment:nginx-1.24.0
在宿主机创建 Nginx挂载目录,将Nginx容器内的目录挂载到宿主机
mkdir -p /www/nginx/conf
mkdir -p /www/nginx/html
mkdir -p /www/nginx/logs
cd /www/nginx/conf
vim nginx.conf
nginx.conf
默认配置内容
worker_processes 1;
events {
worker_connections 1024;
}
http {
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;
include /etc/nginx/mime.types;
default_type application/octet-stream;
charset utf-8;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
# 连接超时时间
keepalive_timeout 60;
types_hash_max_size 2048;
types_hash_bucket_size 64;
server_tokens off;
client_max_body_size 16m;
client_body_buffer_size 128k;
client_header_buffer_size 128k;
open_file_cache max=102400 inactive=20s;
open_file_cache_valid 30s;
open_file_cache_min_uses 1;
client_header_timeout 15;
client_body_timeout 15;
reset_timedout_connection on;
send_timeout 15;
#开启压缩功能
gzip on;
gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript image/svg+xml;
gzip_min_length 2k;
gzip_buffers 16 32k;
gzip_http_version 1.1;
gzip_comp_level 6;
gzip_vary on;
gzip_proxied any;
server {
listen 80;
server_name localhost;
location / {
root html;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# favicon.ico
location = /favicon.ico {
log_not_found off;
}
# robots.txt
location = /robots.txt {
log_not_found off;
}
# assets, media
location ~* \.(?:css(\.map)?|js(\.map)?|jpe?g|png|gif|ico|cur|heic|webp|tiff?|mp3|m4a|aac|ogg|midi?|wav|mp4|mov|webm|mpe?g|avi|ogv|flv|wmv)$ {
expires 7d;
}
# svg, fonts
location ~* \.(?:svgz?|ttf|ttc|otf|eot|woff2?)$ {
add_header Access-Control-Allow-Origin "*";
expires 7d;
}
# 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;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
docker run --name some-nginx \
-d \
-p 80:80 \
-v /www/nginx/conf/nginx.conf:/etc/nginx/nginx.conf \
-v /www/nginx/www:/usr/share/nginx \
-v /www/nginx/logs/nginx:/var/log/nginx \
registry.cn-shenzhen.aliyuncs.com/liyang_main/java_environment:nginx-1.24.0
容器目录说明:
/etc/nginx/nginx.conf
:Nginx配置所在路径
/usr/share/nginx
:Nginx 静态资源所在路径
/var/log/nginx
: Nginx日志所在路径
使用Docker部署Nginx有时候会有反向代理的需求,可以试试这个
假设宿主机有一个接口服务地址为: 127.0.0.1:8080,如果直接在Docker部署的Nginx当中配置:
# 反向代理
location /api{
proxy_connect_timeout 15s;
proxy_send_timeout 15s;
proxy_read_timeout 15s;
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;
proxy_set_header Host $host;
proxy_pass http://127.0.0.1:8080;
}
这样是不生效的,因为docker容器无法访问到宿主机的网络,除非使用的是公网,但是能使用公网就不会用到代理了
需要使用 docker 虚拟桥接网络接口docker0
中的ip地址,它允许Docker容器内通过这个接口与宿主机以及其他容器通信
查看虚拟桥接网络接口
ip addr show docker0
输出
docker0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default
link/ether 02:42:ac:11:00:02 brd ff:ff:ff:ff:ff:ff
inet 172.17.0.1/16 brd 172.17.255.255 scope global docker0
valid_lft forever preferred_lft forever
inet6 fe80::42:acff:fe11:2/64 scope link
valid_lft forever preferred_lft forever
172.17.0.1
这个ip就是docker虚拟桥接网络接口连接宿主机的ip
修改Nginx配置
# 反向代理
location /api{
proxy_connect_timeout 15s;
proxy_send_timeout 15s;
proxy_read_timeout 15s;
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;
proxy_set_header Host $host;
proxy_pass http://172.17.0.1:8080;
}
这样就可以了