一、前置条件
- 安装docker
参考docker安装
二.安装
- 1.下载nginx
docker pull nginx:1.19
- 2.创建共享文件
mkdir /docker_data/nginx/html #html文件存放路径
mkdir /docker_data/nginx/conf #配置文件路径
mkdir /docker_data/nginx/logs #日志路径
在配置文件路径下放入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;
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;
upstream prod-api {
server 172.17.0.1:10000 weight=5;
}
server {
listen 80;
server_name localhost;
location / {
root /usr/share/nginx/html;
index index.html
try_files $uri $uri/ /index.html;
}
location ^~/prod-api/{
proxy_pass http://prod-api/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
}
- 3.创建并启动
docker run -d -p 8080:80 --name nginx_1.19 -v /docker_data/nginx/html:/usr/share/nginx/html -v /docker_data/nginx/conf/nginx.conf:/etc/nginx/nginx.conf -v /docker_data/nginx/logs:/var/log/nginx nginx:1.19
在html路径下放入网页代码,即可通过127.0.0.1:8080访问网页