- 执行下列代码
home=/home/docker/config/nginx;
mkdir -p ${home}/conf;
mkdir -p ${home}/cert;
mkdir -p ${home}/logs;
mkdir -p ${home}/www;
touch ${home}/conf/nginx.conf;
touch ${home}/logs/error.log;
touch ${home}/logs/access.log;
chmod 777 ${home}/conf/nginx.conf;
chmod 777 ${home}/logs/error.log;
chmod 777 ${home}/logs/access.log;
chmod 777 ${home}/conf;
chmod 777 ${home}/cert;
chmod 777 ${home}/logs;
chmod 777 ${home}/www;
- 将下面复制进nginx.conf
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
worker_rlimit_nofile 102400;
events {
worker_connections 102400;
}
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" "$request_body"';
access_log /var/log/nginx/access.log main;
client_max_body_size 500m;
#nginx的默认配置,其client_header_buffer_size的默认值是1k
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
sendfile on;
#tcp_nopush on;
keepalive_timeout 75;
gzip on;
include /etc/nginx/conf.d/*.conf;
proxy_cache_path /var/cache/nginx/cache levels=1:2 keys_zone=imgcache:100m inactive=1d max_size=10g;
#######################################xxxxxxxxx###########################################
server {
listen 9000;
server_name localhost;
location / {
root /usr/share/nginx/html/lots;
index index.html index.htm;
autoindex on;
try_files $uri $uri/ /index.html =404;
}
location /api/ {
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
#proxy_set_header Cookie "$http_cookie; $request_body";
proxy_pass http://localhost:8080/; #末尾加/请求为 localhost:8080/xxx 不加为localhost:8080/api/xxx
}
}
}
- 执行下列命令
home=/home/docker/config/nginx;
docker run --name nginx -d --restart=always --net=host -v ${home}/www:/usr/share/nginx/html:ro -v ${home}/logs:/var/log/nginx -v ${home}/conf/nginx.conf:/etc/nginx/nginx.conf -v ${home}/cert:/etc/nginx/cert -d --privileged=true nginx:1.9.0;
–net=host 的含义是容器内的端口全部映射宿主机端口