一 拉取nginx镜像
docker pull nginx
不写版本的话默认是最新版本
二.创建文件挂载目录
mkdir -p /xj/nginx/{conf,conf.d,html,logs}
三.创建配置文件
1.在 /xj/nginx/conf/下创建配置文件 nginx.conf,然后将一下内容复制进去
user root;
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;
}
2.在/xj/nginx/conf.d目录下创建 default.conf 文件,将一下配置粘贴进去
server {
listen 80;
listen [::]:80;
server_name localhost;
#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;
#}
}
说明:
1.第一个配置文件里的第一行是用户配置,需要和启动用户一致,我的服务器是root用户,这里就用root,如果不一直可能会出现403的问题;
2.nginx是先加载一个主配置文件nginx.conf,在nginx.conf里再加载conf.d目录下的子配置文件(一般最少一个default.conf文件),所以我们一般将需要的配置信息写在default.conf文件中.
四.运行nginx容器
docker run --name nginx -d -p 90:80 -v /xj/nginx/html:/usr/share/nginx/html -v /xj/nginx/conf/nginx.conf:/etc/nginx/nginx.conf:ro -v /xj/nginx/conf.d:/etc/nginx/conf.d nginx:1.23
五.验证是否配置成功
在/xj/nginx/html目录下新建一个index.html文件,内容为 hello nginx,然后访问 服务器地址:端口