centos7 使用docker 部署nginx
conf配置文件
nginx.conf最重要的文件,配置
yml的格式配置文件
server xx;//监听端口,一个端口只能被一个程序所占用,先到先得
server_name localhost;//localhost为域名地址
location / {
root html
index index.html //入口文件
}
html网站文件
index网页
第一步:使用docker获取nginx
docker pull nginx # 获取nginx镜像
第二步:然后启动nginx或者使用参数挂载启动
docker run -itd --name ng nginx #启动容器,保持在后台运行,指定名字为ng
或:
以下是我自定义的部署
docker run -idt --name ng -p 80:8000 -v /root/ng/html:/usr/share/nginx/html -v /root/ng/nginx.conf:/etc/nginx/nginx.conf nginx
-p 参数为指定挂载端口,左边为虚拟机主机端口,右边8000为容器端口
-v 参数为挂载位置,/root/ng/html为挂载的名为ng的nginx容器的虚拟机主机的新建的ng文件夹下的html文件夹,挂载的地方为容器的/usr/share/nginx/html文件夹,指定配置文件nginx.conf挂载目录在虚拟机主机的新建文件夹ng下的nginx.conf文件挂载,冒号右边的为容器/etc/nginx/nginx.conf
这样就不会读取容器内的nginx.conf文件,而是读取虚拟机主机的被挂载文件
以下为nginx.conf配置文件内容:
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include 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 logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 8000; #监听端口,一个端口只能被一个程序占用,先到先得,这里的listen是我挂载的端口设置为8000
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root /usr/share/nginx/html;#这里挂载的是容器地址的html文件的绝对路径
index index.html index.html;#这里挂载的是html文件夹下的index.html
}
#error_page 404 /404.html;
<pre><code> # 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;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
docker inspect ng #查看ng容器的信息,主要是找到ipaddr
直接在虚拟机访问浏览器,直接访问上一步的ip地址,默认为80端口
刚进入容器,编辑配置文件命令为-bash vim:not commond found
是因为不知道命令,无法识别,需要先更新软件源
apt-get update
然后下载vim,或者其他相关的软件包
apt install vim
容器内编辑的配置文件,与挂载的目录文件一致(虽然说nginx是热部署),但是需要重新启动部署的nginx服务,才能让windows访问到虚拟机中docker部署的nginx
docker exec -it ng bash #进入容器初始化
编辑配置文件内容(虚拟机主机下)
vi /root/ng/nginx.conf
修改server中的监听端口为8000(我配置的nginx挂载端口为8000)
server {
listen 8000; #监听端口,一个端口只能被一个程序占用,先到先得,这里的listen是我挂载的端口设置为8000
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root /usr/share/nginx/html;#这里挂载的是容器地址的html文件的绝对路径
index index.html index.html;#这里挂载的是html文件夹下的index.html
}
然后使用windows的浏览器访问虚拟机地址,加上端口,这样就能访问到docker部署的nginx服务了~
评论区