1. 下载镜像
docker pull nginx
2. 查看本地镜像
docker images
3. 运行容器
- 将本地的nginx配置文件挂载到容器中方便修改,提前创建好配置文件
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log notice;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
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"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
}
docker run --name my-nginx -v D:/docker/nginx/conf/nginx.conf:/etc/nginx/nginx.conf -p 9090:80 -d nginx
4. 查看运行的容器
docker ps
5. 反向代理
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log notice;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
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"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
server {
listen 80;
server_name 192.168.110.85; # 访问的ip
location / {
proxy_pass https://www.baidu.com/; # 被代理服务地址
proxy_set_header X-Real-IP 192.111.111.111; # 传递客户端真实IP
proxy_set_header X-Forwarded-For 192.111.111.111; # 传递X-Forwarded-For头
proxy_redirect off;
}
}
}
- 访问http://192.168.110.85:9090/