注意
- nginx.conf第一行是user root才能訪問到逗余,不能是user nginx,不然或報403錯誤
1. 創(chuàng)建將要掛載的目錄(將自己的配置文件掛載docker安裝的nginx默認配置)
mkdir -p /usr/nginx/{conf,conf.d,html,logs}
2. 先要有配置文件才能啟動容器蒋纬,創(chuàng)建配置文件(不用修改默認目錄猎荠,下面會掛載,初始文件就行)
vim /usr/nginx/conf/nginx.conf
user root;
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;
}
vim /usr/nginx/conf.d/default.conf
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
# 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;
#}
}
3. 為了方便測試蜀备,創(chuàng)建一個test.html文件
vi /usr/nginx/html/test.html
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>請求成功关摇!</h1>
</body>
</html>
4. 啟動容器(沒下載鏡像會自動下載)
docker run --name nginx -d -p 80:80 -v /usr/nginx/html:/usr/share/nginx/html:ro -v /usr/nginx/conf/nginx.conf:/etc/nginx/nginx.conf:ro -v /usr/nginx/conf.d:/etc/nginx/conf.d:ro -v /usr/nginx/logs:/var/log/nginx nginx
- 第一個-v后是將本地的這個存放靜態(tài)資源的目錄掛載到nginx的默認加載目錄
- 第二個-v后是將本地的nginx.conf配置文件掛載nginx的默認配置文件
- 第三個-v后是將nginx的默認日志文件與自定義的這個目錄可以讀寫
- 第四個-v后是將本地的conf.d配置文件夾掛載nginx的默認配置文件夾,里面有default.conf配置文件碾阁,負載均衡和反向代理也在這里配置
- ro表示只能讀输虱,rw是讀寫,不設置rw=true脂凶,可以使用
docker inspect nginx
查看- 掛載之后可以使用
docker exec -it nginx bash
進入容器查看是否已掛載宪睹,例如愁茁,進入/usr/share/nginx/html查看里面是否有主機/usr/nginx/html里面的文件
當進入容器出現(xiàn)
bash: vi: command not found
錯誤時,這是因為vim沒有安裝亭病,使用如下命令安裝:
apt-get update
apt-get install vim