1.下载nginx
[root@localhost ~]# docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
54fec2fa59d0: Pull complete
4ede6f09aefe: Pull complete
f9dc69acb465: Pull complete
Digest: sha256:86ae264c3f4acb99b2dee4d0098c40cb8c46dcf9e1148f05d3a51c4df6758c12
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
heidaodageshiwo/jenkins0_mjg lts 5fc78c796d8c 9 days ago 594MB
nginx latest 602e111c06b6 2 weeks ago 127MB
openjdk latest 0ce6496aae74 3 weeks ago 497MB
tomcat 8.0.52 b4b762737ed4 22 months ago 356MB
2.启动nginx
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
heidaodageshiwo/jenkins0_mjg lts 5fc78c796d8c 9 days ago 594MB
nginx latest 602e111c06b6 2 weeks ago 127MB
openjdk latest 0ce6496aae74 3 weeks ago 497MB
tomcat 8.0.52 b4b762737ed4 22 months ago 356MB
[root@localhost ~]# docker run -id -p 80:80 nginx
7549a1306c9f1022b1ecf4f403342de7e58fdb98dc4855c32a1741102ee59231
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
7549a1306c9f nginx "nginx -g 'daemon of…" 3 seconds ago Up 2 seconds 0.0.0.0:80->80/tcp elegant_dhawan
[root@localhost ~]#
浏览器访问
启动成功
后记:
重启nginx容器: docker restart 容器id
关闭nginx容器: docker stop 容器id
进入nginx容器:docker exec -it 容器id /bin/bash
[root@localhost ~]# docker run -id -p 80:80 nginx
7549a1306c9f1022b1ecf4f403342de7e58fdb98dc4855c32a1741102ee59231
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
7549a1306c9f nginx "nginx -g 'daemon of…" 3 seconds ago Up 2 seconds 0.0.0.0:80->80/tcp elegant_dhawan
[root@localhost ~]# docker restart 7549a1306c9f
7549a1306c9f
[root@localhost ~]# docker stop docker 7549a1306c9f
7549a1306c9f
Error response from daemon: No such container: docker
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
7549a1306c9f nginx "nginx -g 'daemon of…" 5 minutes ago Exited (0) 23 seconds ago elegant_dhawan
[root@localhost ~]# docker start 7549a1306c9f
7549a1306c9f
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
7549a1306c9f nginx "nginx -g 'daemon of…" 6 minutes ago Up 14 seconds 0.0.0.0:80->80/tcp elegant_dhawan
[root@localhost ~]# docker stop 7549a1306c9f
7549a1306c9f
[root@localhost ~]# docker exec -it 7549a1306c9f /bin/bash
Error response from daemon: Container 7549a1306c9f1022b1ecf4f403342de7e58fdb98dc4855c32a1741102ee59231 is not running
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@localhost ~]# docker start 7549a1306c9f
7549a1306c9f
[root@localhost ~]# docker exec -it 7549a1306c9f /bin/bash
root@7549a1306c9f:/# ls
bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
root@7549a1306c9f:/# ll
bash: ll: command not found
root@7549a1306c9f:/# ls
bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
root@7549a1306c9f:/#
nginx安装目录
find / -name nginx
挂载容器目录启动nginx容器
[root@localhost ~]# docker run -it --name=myNginx -v /home/data:/etc/nginx -p 80:80 nginx
nginx.conf 介绍:
基本配置:
user nginx; #配置worker进程运行用户
worker_processes 1; #配置工程进程数目,根据硬件配置,一般是和CPU数量一致,或者CPU数量的2倍,能达到最佳性能
error_log /var/log/nginx/error.log warn; # 配置全局错误日志文件以及配置级别 [ debug | info | notice | warn | error | crit ]
pid /var/run/nginx.pid; #配置进程pid文件
关于日志级别:
在配置nginx.conf 的时候,有一项是指定错误日志的,默认情况下你不指定也没有关系,因为nginx很少有错误日志记录的。但有时出现问题时,是有必要记录一下错误日志的,方便我们排查问题。
error_log 级别分为 debug, info, notice, warn, error, crit 默认为crit, 该级别在日志名后边定义格式如下:
error_log /your/path/error.log crit;
crit 记录的日志最少,而debug记录的日志最多。如果你的nginx遇到一些问题,比如502比较频繁出现,但是看默认的error_log并没有看到有意义的信息,那么就可以调一下错误日志的级别,当你调成error级别时,错误日志记录的内容会更加丰富。
events 配置:
events 是配置工作模式和连接数
events {
worker_connections 1024; # 配置每个worker进程连接数上限
}
说明:nginx支持得总连接数=worker_processes * worker_connections
http基本配置:
配置http服务器
配置http服务器
http {
include /etc/nginx/mime.types; # 配置nginx支持哪些多媒体类型
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; #开启gzip压缩输出
include /etc/nginx/conf.d/*.conf;
}
配置server服务器;可以多个
配置server服务器;可以多个;
server {
listen 80; #监听端口
server_name localhost; # 配置服务名
#charset koi8-r; #配置字符集
#access_log /var/log/nginx/host.access.log main; #配置本虚拟主机访问日志
# 匹配/请求 ,/是根路径请求,会被该location匹配到并且处理
location / {
root /usr/share/nginx/html; #root是配置服务器的默认网关根目录位置
index index.html index.htm; #配置首页文件的名称
}
#error_page 404 /404.html; #配置404页面
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html; #配置50x错误页面
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;
#}
}