修改配置注意
- nginx.conf第一行是user root才能访问到,不能是user nginx,不然或报403错误
1. 创建将要挂载的目录(将自己的配置文件挂载docker安装的nginx默认配置)
mkdir -p /usr/nginx/{conf,conf.d,html,logs}
2. 先要有配置文件才能启动容器,创建配置文件(不用修改默认目录,下面会挂载,初始文件就行)
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. 为了方便测试,创建一个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后是将本地的这个存放静态资源的目录挂载到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里面的文件
当进入容器出现
bash: vi: command not found
错误时,这是因为vim没有安装,使用如下命令安装:
apt-get update
apt-get install vim
网友评论