Nginx容器初始化与源码安装
首先启动一个名为nginx1的Docker容器,基于centos:latest镜像,并进入交互式bash环境。
[root@server1 ~]# docker run -it --name nginx1 -v /opt/data/web2:/web -p 81:80 centos:latest /bin/bash
[root@608de4875036 /]#
进入容器内的web目录,使用wget命令下载Nginx 1.20.1版本的源码包。
[root@608de4875036 web]# wget http://nginx.org/download/nginx-1.20.1.tar.gz
解压下载的压缩包,确认目录下存在nginx-1.20.1文件夹及对应的tar.gz文件。
[root@608de4875036 web]# ls
nginx-1.20.1 nginx-1.20.1.tar.gz
安装编译Nginx所需的依赖包,包括pcre-devel、openssl-devel、gd-devel以及gcc等开发工具组。
[root@608de4875036 web]# yum -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++ make
[root@608de4875036 web]# yum -y groups mark install 'Development Tools'
创建一个名为nginx的系统用户,用于运行Nginx服务,该用户无家目录且不可登录。
[root@608de4875036 web]# useradd -r -M -s /sbin/nologin nginx
[root@608de4875036 web]# id nginx
uid=998(nginx) gid=996(nginx) groups=996(nginx)
在web目录下创建log目录,专门用于存放Nginx的运行日志文件。
[root@6ad47178bdd6 web]# mkdir log
进入解压后的nginx-1.20.1目录,执行configure脚本进行编译配置,指定安装路径、用户组及模块支持,最后执行make && make install完成安装。
[root@608de4875036 web]# ls
log nginx-1.20.1 nginx-1.20.1.tar.gz
[root@608de4875036 web]# cd nginx-1.20.1
[root@608de4875036 nginx-1.20.1]# ls
auto conf html README
CHANGES configure LICENSE src
CHANGES.ru contrib man
[root@608de4875036 nginx-1.20.1]# ./configure \
--prefix=/web/nginx \
--user=nginx \
--group=nginx \
--with-debug \
--with-http_ssl_module \
--with-http_realip_module \
--with-http_image_filter_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_stub_status_module \
--http-log-path=/web/log/access.log \
--error-log-path=/web/log/error.log
[root@608de4875036 nginx-1.20.1]# make && make install
查看web目录结构,确认已生成nginx安装目录,包含conf、html、logs和sbin等子目录。
[root@6ad47178bdd6 web]# ls
log nginx nginx-1.20.1 nginx-1.20.1.tar.gz
[root@608de4875036 web]# cd nginx
[root@608de4875036 nginx]# ls
conf html logs sbin
将Nginx的可执行文件路径添加到系统环境变量中,使其可以在任意位置直接调用nginx命令。
[root@608de4875036 nginx]# ls
conf html logs sbin
[root@608de4875036 nginx]# cd sbin/
[root@608de4875036 sbin]# ls
nginx
[root@608de4875036 sbin]# pwd
/web/nginx/sbin
[root@608de4875036 sbin]# echo "export PATH=/web/nginx/sbin:\$PATH" > /etc/profile.d/nginx.sh
[root@608de4875036 sbin]# source /etc/profile.d/nginx.sh
[root@608de4875036 sbin]# which nginx
/web/nginx/sbin/nginx
启动Nginx服务,并通过ss命令检查监听状态,确认80端口已成功监听。
[root@608de4875036 sbin]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:80 0.0.0.0:*
在容器内部查看nginx目录下的日志和临时文件目录,确认服务正常运行产生的文件结构。
[root@608de4875036 web]# ls
nginx nginx-1.20.1 nginx-1.20.1.tar.gz
[root@608de4875036 web]# cd nginx
[root@608de4875036 nginx]# ls
client_body_temp html sbin
conf logs scgi_temp
fastcgi_temp proxy_temp uwsgi_temp
切换到宿主机,进入挂载点/opt/data/web2目录,查看其中的文件列表。
[root@server1 ~]# cd /opt/data/
[root@server1 data]# ls
web1 web2
[root@server1 data]# cd web2
[root@server1 web2]# ls
log nginx nginx-1.20.1 nginx-1.20.1.tar.gz
[root@server1 web2]# cd nginx
[root@server1 nginx]# ls
client_body_temp html sbin
conf logs scgi_temp
fastcgi_temp proxy_temp uwsgi_temp
数据卷挂载与配置同步验证
对比发现,宿主机上的web2目录内容与容器内nginx目录完全一致,证明数据卷挂载生效,数据已同步。
在宿主机的web2目录中修改Nginx配置文件,以验证外部修改能否即时影响容器内的服务。
创建一个名为xy的目录,并将游戏相关的代码文件复制到此目录下,作为测试网页内容。
[root@server1 html]# pwd
/opt/data/web2/nginx/html
[root@server1 html]# mkdir yx
[root@server1 html]# cd yx
[root@server1 yx]# ls
image index.html js
同时创建另一个名为test的目录,用于后续的配置测试或资源隔离。
[root@server1 html]# mkdir test
[root@server1 html]# ls
[root@server1 html]# ls
50x.html index.html test yx
[root@server1 html]# mv 50x.html index.html test/
[root@server1 html]# ls
test yx
编辑nginx.conf配置文件,调整相关设置以适配新的目录结构和访问需求。
[root@server1 conf]# vi nginx.conf
........
server {
listen 8080;
server_name test.example.com;
location / {
root /web/nginx/html/test; #容器内文件地址
index index.html index.htm;
}
}
server {
listen 80;
server_name xy.example.com;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root /web/nginx/html/yx; #容器内文件地址
index index.html index.htm;
}
.....
然而,当前的配置仅映射了一个端口,导致部分服务无法通过其他端口访问,存在局限性。
[root@server1 conf]# docker port 608de4875036
80/tcp -> 0.0.0.0:81
80/tcp -> :::81
多端口映射问题解决与重启
为了解决多端口访问的问题,需要采取进一步的措施来完善网络映射配置。
删除当前运行的nginx1容器,以便重新创建并应用新的端口映射策略。
[root@server1 ~]# docker stop 608de4875036
608de4875036
[root@server1 ~]# docker rm 608de4875036
608de4875036
尽管容器被删除,但由于使用了数据卷挂载,宿主机/opt/data/web2中的数据依然保留完好。
[root@server1 web2]# ls
log nginx nginx-1.20.1 nginx-1.20.1.tar.gz
重新创建一个新的容器实例,再次映射宿主机目录,并确保包含所有需要的端口映射。
[root@server1 ~]# docker run -it --name nginx2 -v /opt/data/web2:/web -p 80:80 -p 8080:8080 centos:latest /bin/bash
[root@6ad47178bdd6 /]#
在宿主机上检查新容器的状态及挂载情况,确认配置是否正确加载。
[root@server1 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6ad47178bdd6 centos:latest "/bin/bash" 23 seconds ago Up 22 seconds 0.0.0.0:80->80/tcp, :::80->80/tcp, 0.0.0.0:8080->8080/tcp, :::8080->8080/tcp nginx2
[root@server1 ~]# docker port 6ad47178bdd6
80/tcp -> 0.0.0.0:80
80/tcp -> :::80
8080/tcp -> 0.0.0.0:8080
8080/tcp -> :::8080
进入新创建的容器内部,检查数据是否同步,确认之前的配置文件和网页内容依然存在。
[root@6ad47178bdd6 /]# ls
bin home lost+found opt run sys var
dev lib media proc sbin tmp web
etc lib64 mnt root srv usr
[root@6ad47178bdd6 /]# cd web/
[root@6ad47178bdd6 web]# ls
nginx nginx-1.20.1 nginx-1.20.1.tar.gz
[root@6ad47178bdd6 web]# cd nginx
[root@6ad47178bdd6 nginx]# ls
client_body_temp html sbin
conf logs scgi_temp
fastcgi_temp proxy_temp uwsgi_temp
#数据以同步
在新容器内启动Nginx服务,确保服务能够正常读取配置并运行。
#写一个环境变量
[root@6ad47178bdd6 /]# cat /etc/profile.d/nginx.sh
export PATH=/web/nginx/sbin:$PATH
#创建nginx用户
[root@6ad47178bdd6 /]# useradd -r -M -s /sbin/nologin nginx
[root@6ad47178bdd6 /]# nginx
[root@6ad47178bdd6 /]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:80 0.0.0.0:*
LISTEN 0 128 0.0.0.0:8080 0.0.0.0:*
在浏览器或客户端访问IP地址192.168.244.145的80端口,验证主服务是否正常响应。

同时访问192.168.244.145的8080端口,确认额外映射的端口也能正常提供服务,解决之前端口缺失的问题。

到此这篇关于nginx容器配置文件独立的实现的文章就介绍到这了,更多相关nginx容器配置文件独立内容请搜索本站以前的文章或继续浏览下面的相关文章希望大家以后多多支持本站!