Independent implementation of nginx container configuration file

Independent implementation of nginx container configuration file

Create a container

[root@server1 ~]# docker run -it --name nginx1 -v /opt/data/web2:/web -p 81:80 centos:latest /bin/bash
[root@608de4875036 /]#

Enter the web directory and download the nginx package

[root@608de4875036 web]# wget http://nginx.org/download/nginx-1.20.1.tar.gz

Unzip the directory

[root@608de4875036 web]# ls
nginx-1.20.1 nginx-1.20.1.tar.gz

Install dependency packages

[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'

Create User

[root@608de4875036 web]# useradd -r -M -s /sbin/nologin nginx
[root@608de4875036 web]# id nginx
uid=998(nginx) gid=996(nginx) groups=996(nginx)

Create a log storage file address

[root@6ad47178bdd6 web]# mkdir log

Compile and 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

View Catalog

[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

Configuring environment variables

[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

Start the service

[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:*   

View services on the container

[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

View on the host machine

[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

You can see that the data has been synchronized

Modify the configuration file on the host

Create a directory named xy and copy the game code into this directory

[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

Create a directory 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

Modify the nginx.conf configuration file

[root@server1 conf]# vi nginx.conf

........

    server {
         listen 8080;
         server_name test.example.com;

         location / {
             root /web/nginx/html/test; #File address in the container 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; #File address in the container index index.html index.htm;
        }
.....

But there is a problem with this modification. Only one port is mapped, and the other port is not mapped.

[root@server1 conf]# docker port 608de4875036
80/tcp -> 0.0.0.0:81
80/tcp -> :::81

How to resolve it?

Delete this container

[root@server1 ~]# docker stop 608de4875036
608de4875036
[root@server1 ~]# docker rm 608de4875036
608de4875036

The data here is still on the host machine

[root@server1 web2]# ls
log nginx nginx-1.20.1 nginx-1.20.1.tar.gz

Recreate this directory as a container mapping

[root@server1 ~]# docker run -it --name nginx2 -v /opt/data/web2:/web -p 80:80 -p 8080:8080 centos:latest /bin/bash
[root@6ad47178bdd6 /]#

View on the host machine

[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

Check whether the data is synchronized in the container

[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
#Data synchronization

Start the service

#Write an environment variable [root@6ad47178bdd6 /]# cat /etc/profile.d/nginx.sh 
export PATH=/web/nginx/sbin:$PATH
#Create nginx user [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:*   

Visit 192.168.244.145:80

insert image description here

Visit 192.168.244.145:8080

insert image description here

This is the end of this article about the implementation of independent nginx container configuration files. For more related independent nginx container configuration files, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Nginx configuration file detailed explanation and optimization suggestions guide
  • How to view nginx configuration file path and resource file path
  • Detailed explanation of Nginx configuration file
  • Detailed explanation of nginx configuration file interpretation
  • Detailed explanation of Nginx static file service configuration and optimization

<<:  A brief discussion on the invalidation or implicit conversion of MySQL integer and string indexes

>>:  A brief discussion on HTML titles, paragraphs, line breaks, horizontal lines, and special characters

Recommend

Detailed explanation of MySQL semi-synchronization

Table of contents Preface MySQL master-slave repl...

How to Communicate with Other Users on the Linux Command Line

It's easy to send messages to other users in ...

HTML embedded in WMP compatible with Chrome and IE detailed introduction

In fact, there are many corresponding writing met...

Docker uses the Prune command to clean up the none image

Table of contents The creation and confusion of n...

A brief discussion on CSS blocking merging and other effects

Non-orthogonal margins When margin is used, it wi...

Detailed example of using the distinct method in MySQL

A distinct Meaning: distinct is used to query the...

Some functions of using tcpdump to capture packets in the Linux command line

tcpdump is a flexible and powerful packet capture...

How to solve the problem of automatic package update in Debian system

I don't know when it started, but every time ...

Understanding MySQL precompilation in one article

1. Benefits of precompilation We have all used th...

Detailed explanation of common methods of JavaScript arrays

Table of contents Common array methods pop() unsh...

Detailed explanation of special phenomena examples of sleep function in MySQL

Preface The sleep system function in MySQL has fe...

jQuery realizes the scrolling effect of table row data

This article example shares the specific code of ...

Tomcat Nginx Redis session sharing process diagram

1. Preparation Middleware: Tomcat, Redis, Nginx J...