docker安装nginx,并且conf、html目录挂载到宿主机。
html修改实时生效;
conf.d目录docker restart nginx容器ID
生效。
cd /opt
mkdir nginx_volume_data
cd nginx_volume_data
mkdir conf
cd conf
#内容见:nginx.conf
touch nginx.conf
mkdir conf.d
cd conf.d
#内容见:default.conf
touch default.conf
cd ../../
mkdir html
cd html
#内容见:index.html
touch index.html
#80端口示例
docker run -p 80:80 --name nginx -v /opt/nginx_volume_data/html:/usr/share/nginx/html -v /opt/nginx_volume_data/conf/nginx.conf:/etc/nginx/nginx.conf -v /opt/nginx_volume_data/conf/conf.d:/etc/nginx/conf.d -d nginx
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;
client_max_body_size 20m;
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;
#开启gzip压缩
gzip on;
#http的协议版本
gzip_http_version 1.0;
#IE版本1-6不支持gzip压缩,关闭
gzip_disable 'MSIE[1-6].';
#需要压缩的文件格式 text/html默认会压缩,不用添加
gzip_types text/css text/javascript application/javascript image/jpeg image/jpg image/png image/gif;
#设置压缩缓冲区大小,此处设置为4个8K内存作为压缩结果流缓存
gzip_buffers 4 8k;
#压缩文件最小大小
gzip_min_length 1k;
#压缩级别1-9
gzip_comp_level 9;
#给响应头加个vary,告知客户端能否缓存
gzip_vary on;
#反向代理时使用
gzip_proxied off;
include /etc/nginx/conf.d/*.conf;
}
default.conf:
80端口示例
server {
listen 80;
server_name 192.168.1.105;
charset utf-8;
#proxy_intercept_errors on;
#ui前端
location / {
root /usr/share/nginx/html/dist;
index index.html index.htm;
}
#server服务端口
location ^~/api {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Real-Port $remote_port;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_buffering off;
rewrite ^/api/(.*)$ /$1 break;
proxy_pass http://192.168.1.105:7799;
}
}
index.html:
NGINX默认index页面
<!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>
实战
#如果有80,8888,9999,7777,2222端口
docker run -p 80:80 -p 8888:8888 -p 9999:9999 -p 7777:7777 -p 2222:2222 --name nginx \
-v /opt/nginx_volume_data/html:/usr/share/nginx/html \
-v /opt/nginx_volume_data/conf/nginx.conf:/etc/nginx/nginx.conf \
-v /opt/nginx_volume_data/conf/conf.d:/etc/nginx/conf.d \
-e TZ=Asia/Shanghai \
-d nginx