https://hub.docker.com/searchq=nginx&type=image
一、查看docker是否安装nginx
docker images
二、安装nginx
docker pull nginx
三、运行nginx镜像
docker run --name mynginx -p 80:80 -d nginx
四、查看运行结果
docker ps
网页访问,浏览器输入虚拟机IP:端口号地址回车,就可以看到 “Welcome to nginx!”
centos开放的端口并设置
firewall-cmd --zone=public --list-ports #查看所有开放的端口
firewall-cmd --zone=public --add-port=8080/tcp --permanent # 开放5672端口
firewall-cmd --zone=public --remove-port=8080/tcp --permanent #关闭5672端口
firewall-cmd --reload # 配置立即生效
systemctl start/stop/restart firewalld.service #打开/关闭/重启防火墙
firewall-cmd --state #查看防火墙状态
五、把nginx 配置文件映射到虚拟机(每次改配置都需要到镜像里改,太麻烦!!!)
(1)本地创建管理目录
mkdir -p /data/nginx
mkdir -p /data/nginx/www
mkdir -p /data/nginx/conf
mkdir -p /data/nginx/logs
(2)将容器中的相应文件copy到刚创建的管理目录中
docker cp 6f37f231a7c1:/etc/nginx/nginx.conf /data/nginx/
docker cp 6f37f231a7c1:/etc/nginx/conf.d /data/nginx/conf/
docker cp 6f37f231a7c1:/usr/share/nginx/html/ /data/nginx/www/
docker cp 6f37f231a7c1:/var/log/nginx/ /data/nginx/logs/
注:docker cp 6f37f231a7c1中的 "6f37f231a7c1" 为容器ID(docker ps可查看)
(3)停止并移除容器
docker stop 6f37f231a7c1 #停止容器
docker rm 6f37f231a7c1 #移除容器
(4)再次启动容器并作目录挂载
docker run --name nginx -p 80:80 -v /data/nginx/nginx.conf:/etc/nginx/nginx.conf -v /data/nginx/www/:/usr/share/nginx/html/ -v /data/nginx/logs/:/var/log/nginx/ -v /data/nginx/conf/:/etc/nginx/conf.d --privileged=true -d nginx
ps:-p 80:80 端口进行映射,将本地 80端口映射到容器内部的 80 端口。
六、我自己测试的配置文件nginx.conf配置
user nginx;
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;
upstream mynginx{
server 192.168.100.164:1111 weight=1;
server 192.168.100.164:2222 weight=1;
}
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
#root html;
proxy_pass http://mynginx;
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 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;
#}
}
include /etc/nginx/conf.d/*.conf;
}
先自我介绍一下,小编13年上师交大毕业,曾经在小公司待过,去过华为OPPO等大厂,18年进入阿里,直到现在。深知大多数初中级java工程师,想要升技能,往往是需要自己摸索成长或是报班学习,但对于培训机构动则近万元的学费,着实压力不小。自己不成体系的自学效率很低又漫长,而且容易碰到天花板技术停止不前。因此我收集了一份《java开发全套学习资料》送给大家,初衷也很简单,就是希望帮助到想自学又不知道该从何学起的朋友,同时减轻大家的负担。添加下方名片,即可获取全套学习资料哦